달력

3

« 2025/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2011. 11. 10. 12:18

Selector.open() 뜯어보자 JAVA이야기2011. 11. 10. 12:18

- selector.open();

public static Selector open() throws IOException {
return SelectorProvider.provider().openSelector();
    }
 
public static SelectorProvider provider() {
synchronized (lock) { //client lock를 걸어 동기화한다.
   if (provider != null)
return provider;
   return (SelectorProvider)AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
   if (loadProviderFromProperty()) //System property로 부터 Provider를 로드한다.
return provider;
   if (loadProviderAsService())  //Service로 부터 Provider를 로드한다.
return provider;
   provider = sun.nio.ch.DefaultSelectorProvider.create();
   return provider;
}
   });
}
    } 

 /**
     * Performs the specified <code>PrivilegedAction</code> with privileges
     * enabled. The action is performed with <i>all</i> of the permissions 
     * possessed by the caller's protection domain.
     * 
     * <p> If the action's <code>run</code> method throws an (unchecked)
     * exception, it will propagate through this method.
     *
     * <p> Note that any DomainCombiner associated with the current
     * AccessControlContext will be ignored while the action is performed.
     *
     * @param action the action to be performed.
     *
     * @return the value returned by the action's <code>run</code> method.
     *
     * @exception NullPointerException if the action is <code>null</code>
     *
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
     * @see #doPrivileged(PrivilegedExceptionAction)
     * @see #doPrivilegedWithCombiner(PrivilegedAction)
     * @see java.security.DomainCombiner
     */

    public static native <T> T doPrivileged(PrivilegedAction<T> action);

public interface PrivilegedAction<T> {
    /**
     * Performs the computation.  This method will be called by
     * <code>AccessController.doPrivileged</code> after enabling privileges.
     *
     * @return a class-dependent value that may represent the results of the
     *       computation. Each class that implements
     *         <code>PrivilegedAction</code>
     *       should document what (if anything) this value represents.
     * @see AccessController#doPrivileged(PrivilegedAction)
     * @see AccessController#doPrivileged(PrivilegedAction,
     *                                     AccessControlContext)
     */
    T run();


 private static boolean loadProviderFromProperty() {
String cn = System.getProperty("java.nio.channels.spi.SelectorProvider");
if (cn == null)
   return false;
try {
   Class c = Class.forName(cn, true,
   ClassLoader.getSystemClassLoader());
   provider = (SelectorProvider)c.newInstance();
   return true;
} catch (ClassNotFoundException x) {
   throw new ServiceConfigurationError(x);
} catch (IllegalAccessException x) {
   throw new ServiceConfigurationError(x);
} catch (InstantiationException x) {
   throw new ServiceConfigurationError(x);
} catch (SecurityException x) {
   throw new ServiceConfigurationError(x);
}
    } 

/*
  아 Service객체는 뭐냐 또..
구글링 해 본결과 이 녀석은 sun.misc에 종속적이다라는 것을 알게 되었다. 알고리즘에 대한 일반적인 인터페이스만 정의하고, 실제 구현은 암호 알고리즘을 가지고 있는 벤더들이 제공할 수 있도록 되어 있다.자바 XML 파서의 경우도 인터페이스만 제공하고, 실제 XML 파서는 여러 벤더의 파서를 사용할 수 있도록 SPI를 제공하고 있다. 
SPI는 보통 플러그인(plugin) 형태로 같은 기능여러 벤더에서 제공할 수 있도록 만든 특수한 프로그래밍 인터페이스(API)이다.  
저는 "하나의 서비스 인터페이스를 등록해두면 이 인터페이스를 벤더들이 각자 구현할 수 있다라고 생각합니다."
 

http://gleamynode.net/articles/1618/
 

*/
 private static boolean loadProviderAsService() {
Iterator i = Service.providers(SelectorProvider.class,
      ClassLoader.getSystemClassLoader());
for (;;) {
   try {
if (!i.hasNext())
   return false;
provider = (SelectorProvider)i.next();
return true;
   } catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
   // Ignore the security exception, try the next provider
   continue;
}
throw sce;
   }
}
    } 
 


public abstract AbstractSelector openSelector()
throws IOException; 

'JAVA이야기' 카테고리의 다른 글

동기화 클래스 구현  (0) 2011.11.11
스레드 팩토리  (0) 2011.11.11
Selector & Channel  (0) 2011.11.09
JAVA NIO  (0) 2011.11.09
POSION OBJECT  (0) 2011.11.08
:
Posted by НooпeУ


Code Start Code End