ldap.java
来自「这是一个基于计算网格的web service。它用java编写。一旦安装完成」· Java 代码 · 共 1,467 行 · 第 1/4 页
JAVA
1,467 行
public static String getProviderUrl() { return providerUrl; } public static void setResMgrDirectory( String dir ) { directory = dir; } public static String getResMgrDir() { return directory; } public static void deleteJobHosts() throws Exception { DirContext context = null; try { Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXTFACTORY ); env.put( Context.PROVIDER_URL, getProviderUrl() ); env.put( Context.OBJECT_FACTORIES, JOBHOST_OBJFACTORY ); env.put( Context.STATE_FACTORIES, JOBHOST_STATEFACTORY ); // get the context context = new InitialDirContext( env ); // create a filter to return all job hosts String[] attrIDs = new String[] { ATTR_CN }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(cn=*)"; NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); try { while( nameEnum.hasMore() ) { SearchResult result = (SearchResult) nameEnum.next(); Attributes attribs = result.getAttributes(); Attribute attr = null; if ( (attr = attribs.get( ATTR_CN )) == null ) { continue; } String escWsAddr = doCharEscape( (String) attr.get() ); context.unbind( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir() ); } } catch ( SizeLimitExceededException se ) {} } catch ( Exception ex ) { throw ex; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception e ) {} } } public static void registerJobHost( String wsAddr, String clusterId, String osName, String osVersion, String osArch, String cpuVendor, String cpuModel, Integer cpuCount, Integer cpuSpeed, Integer cpuUserTime, Integer cpuIdleTime, Integer cpuSysTime, Integer cpuLoad, Integer memTotal, Integer memFree, Integer diskSpaceTot, Integer diskSpaceFree, List<String> jobTypeList ) throws Exception { DirContext context = null; String escWsAddr = wsAddr; try { if ( !wsAddr.startsWith( "http://") ) { escWsAddr = "http://" + wsAddr; } else { escWsAddr = wsAddr; } escWsAddr = doCharEscape( escWsAddr ); Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXTFACTORY ); env.put( Context.PROVIDER_URL, getProviderUrl() ); env.put( Context.OBJECT_FACTORIES, JOBHOST_OBJFACTORY ); env.put( Context.STATE_FACTORIES, JOBHOST_STATEFACTORY ); // get the context context = new InitialDirContext( env ); // create the object to be bound LdapJobHost jobHost = new LdapJobHost( clusterId, osName, osVersion, osArch, cpuVendor, cpuModel, cpuCount, cpuSpeed, cpuUserTime, cpuIdleTime, cpuSysTime, cpuLoad, memTotal, memFree, diskSpaceTot, diskSpaceFree, jobTypeList ); // create search controls String[] attrIDs = new String[] { ATTR_CN }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(" + ATTR_CN + "=" + escWsAddr + ")"; // check if JobHost is already bound NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); if ( nameEnum.hasMore() ) { context.rebind( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir(), jobHost ); } else { context.bind( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir(), jobHost ); } } catch ( Exception ex ) { throw ex; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception ex ) {} } } public static boolean unregisterJobHost( String wsAddr, String clusterId ) throws Exception { DirContext context = null; boolean success = false; String escWsAddr = wsAddr; try { if ( !wsAddr.startsWith( "http://") ) { escWsAddr = "http://" + wsAddr; } else { escWsAddr = wsAddr; } escWsAddr = doCharEscape( escWsAddr ); Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXTFACTORY ); env.put( Context.PROVIDER_URL, getProviderUrl() ); env.put( Context.OBJECT_FACTORIES, JOBHOST_OBJFACTORY ); env.put( Context.STATE_FACTORIES, JOBHOST_STATEFACTORY ); // get the context context = new InitialDirContext( env ); // create search control for canonical name attribute String[] attrIDs = new String[] { ATTR_CN }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(&" + "(" + ATTR_CN + "=" + escWsAddr + ")" + "(" + ATTR_JHCLUSTERID + "=" + clusterId + ")" + ")"; // check if jobhost exists -- if so, unbind it NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); if ( nameEnum.hasMore() ) { context.unbind( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir() ); success = true; } } catch ( Exception ex ) { throw ex; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception ex ) {} } return success; } public static boolean updateJobHostDynamicInfo( String wsAddr, String clusterId, Integer cpuUserTime, Integer cpuIdleTime, Integer cpuSysTime, Integer cpuLoad, Integer memFree, Integer diskSpaceFree ) throws Exception { DirContext context = null; boolean success = false; String escWsAddr = wsAddr; try { if ( !wsAddr.startsWith( "http://") ) { escWsAddr = "http://" + wsAddr; } else { escWsAddr = wsAddr; } escWsAddr = doCharEscape( escWsAddr ); Hashtable env = new Hashtable(); env.put( Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXTFACTORY ); env.put( Context.PROVIDER_URL, getProviderUrl() ); env.put( Context.OBJECT_FACTORIES, JOBHOST_OBJFACTORY ); env.put( Context.STATE_FACTORIES, JOBHOST_STATEFACTORY ); // get the context context = new InitialDirContext( env ); // create search control for canonical name attribute String[] attrIDs = new String[] { ATTR_CN }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(&" + "(" + ATTR_CN + "=" + escWsAddr + ")" + "(" + ATTR_JHCLUSTERID + "=" + clusterId + ")" + ")"; // check if jobhost exists -- if so, update it NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); if ( nameEnum.hasMore() ) { Vector modVector = new Vector(); if ( cpuUserTime != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_USERTIME, cpuUserTime.toString() ) ) ); } if ( cpuIdleTime != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_IDLETIME, cpuIdleTime.toString() ) ) ); } if ( cpuSysTime != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_SYSTIME, cpuSysTime.toString() ) ) ); } if ( cpuLoad != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_LOAD, cpuLoad.toString() ) ) ); } if ( memFree != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHMEM_FREE, memFree.toString() ) ) ); } if ( diskSpaceFree != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHDISK_FREE, diskSpaceFree.toString() ) ) ); } // update attributes if ( modVector.size() > 0 ) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?