📄 ldap.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -