📄 ldap.java
字号:
ModificationItem[] mods = new ModificationItem[ modVector.size() ]; for ( int i = 0; i < modVector.size(); i++ ) { mods[ i ] = (ModificationItem) modVector.elementAt( i ); } context.modifyAttributes( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir(), mods ); success = true; } } } catch ( Exception ex ) { throw ex; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception e ) {} } return success; } public static boolean updateJobHostStaticInfo( String wsAddr, String clusterId, String osName, String osVersion, String osArch, String cpuVendor, String cpuModel, Integer cpuCount, Integer cpuSpeed, Integer memTotal, Integer diskSpaceTot ) 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 ( osName != null && osName.length() > 0 ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHOS_NAME, osName ) ) ); } if ( osVersion != null && osVersion.length() > 0 ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHOS_VERSION, osVersion ) ) ); } if ( osArch != null && osArch.length() > 0 ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHOS_ARCH, osArch ) ) ); } if ( cpuVendor != null && cpuVendor.length() > 0 ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_VENDOR, cpuVendor ) ) ); } if ( cpuModel != null && cpuModel.length() > 0 ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_MODEL, cpuModel ) ) ); } if ( cpuCount != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_COUNT, cpuCount.toString() ) ) ); } if ( cpuSpeed != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHCPU_SPEED, cpuSpeed.toString() ) ) ); } if ( memTotal != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHMEM_TOTAL, memTotal.toString() ) ) ); } if ( diskSpaceTot != null ) { modVector.addElement( new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute( ATTR_JHDISK_TOTAL, diskSpaceTot.toString() ) ) ); } // update attributes if ( modVector.size() > 0 ) { ModificationItem[] mods = new ModificationItem[ modVector.size() ]; for ( int i = 0; i < modVector.size(); i++ ) { mods[ i ] = (ModificationItem) modVector.elementAt( i ); } context.modifyAttributes( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir(), mods ); success = true; } } } catch ( Exception ex ) { throw ex; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception e ) {} } return success; } public static boolean reserveJobHost( String reservedByWsAddr, String jobHostWsAddr ) throws Exception { DirContext context = null; boolean success = false; String escJobHostWsAddr = null; try { if ( !jobHostWsAddr.startsWith( "http://") ) { escJobHostWsAddr = "http://" + jobHostWsAddr; } else { escJobHostWsAddr = jobHostWsAddr; } escJobHostWsAddr = doCharEscape( escJobHostWsAddr ); 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, ATTR_JHRESERVED }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(" + ATTR_CN + "=" + escJobHostWsAddr + ")"; // if jobhost exists and is not reserved, reserve it NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); if ( nameEnum.hasMore() ) { SearchResult result = (SearchResult) nameEnum.next(); Attributes attribs = result.getAttributes(); Attribute attr = attribs.get( ATTR_JHRESERVED ); if ( attr == null ) { ModificationItem[] mods = new ModificationItem[ 1 ]; mods[ 0 ] = new ModificationItem( DirContext.ADD_ATTRIBUTE, new BasicAttribute( ATTR_JHRESERVED, reservedByWsAddr )); context.modifyAttributes( ATTR_CN + "=" + escJobHostWsAddr + "," + getResMgrDir(), mods ); success = true; } else { String currReservation = (String) attr.get(); if ( currReservation.compareTo( reservedByWsAddr ) == 0 ) { success = true; } } } } catch ( Exception e ) { throw e; } finally { try { if ( context != null ) { context.close(); context = null; } } catch ( Exception e ) {} } return success; } public static boolean releaseJobHost( String reservedByWsAddr, String jobHostWsAddr ) throws Exception { DirContext context = null; boolean success = false; String escWsAddr = null; try { if ( !jobHostWsAddr.startsWith( "http://") ) { escWsAddr = "http://" + jobHostWsAddr; } else { escWsAddr = jobHostWsAddr; } 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, ATTR_JHRESERVED }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes( attrIDs ); String filter = "(&(" + ATTR_CN + "=" + escWsAddr + ")((" + ATTR_JHRESERVED + "=*)))"; // if jobhost exists and is reserved, release it // NOTE: currently can only be released by the // jobhost that reserved it NamingEnumeration nameEnum = context.search( getResMgrDir(), filter, ctls ); if ( nameEnum.hasMore() ) { SearchResult result = (SearchResult) nameEnum.next(); Attributes attribs = result.getAttributes(); Attribute attr = attribs.get( ATTR_JHRESERVED ); if ( attr != null ) { String currReservation = (String) attr.get(); if ( currReservation.compareTo( reservedByWsAddr ) == 0 ) { ModificationItem[] mods = new ModificationItem[ 1 ]; mods[ 0 ] = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, new BasicAttribute( ATTR_JHRESERVED )); context.modifyAttributes( ATTR_CN + "=" + escWsAddr + "," + getResMgrDir(), mods ); success = true; } } } } catch ( Exception e ) { throw e; } finally { try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -