⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createjavaschema.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *   - Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer. * *   - Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. * *   - Neither the name of Sun Microsystems nor the names of its *     contributors may be used to endorse or promote products derived *     from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Copyright (c) 2006.  Sun Microsystems. All rights reserved. *  * Creates a schema for storing Java objects according to RFC 2713 * After running this program, you should verify that the schema  * has been updated correctly by using the directory server's  * administration tool. If the schema has not been properly updated,  * use the administration tool to correct it. * * You should first turn off schema-checking at the directory server  * before running this program. * * usage: * java [-Djava.naming.provider.url=<ldap_server_url>] \ *     CreateJavaSchema [-h|-l|-s[n|n41|ad]] [-n<dn>] [-p<passwd>] [-a<auth>]  *       * -h		Print the usage message *  * -l		List the Java schema in the directory *  * -s[n|n41|ad]	Update schema: *                -sn   means use a workaround for schema bugs in *                      pre-4.1 releases of Netscape Directory Server; *  *		  -sn41 means use a workaround for schema bugs in *                      Netscape Directory Server version 4.1; * *		  -sad  means use a workaround for schema bugs in *                      Microsoft Windows 2000 Active Directory * * -n<dn> 	Use <dn> as the distinguished name for authentication *  * -p<passwd>	Use <passwd> as the password for authentication *  * -a<auth>	Use <auth> as the authentication mechanism. Default is "simple". *  * * If neither -s, -l, nor -h has been specified, the default is "-l". * * The following example inserts the Java schema from RFC 2713 in a * Netscape Directory (using the workaround for 4.1 schema bugs), * logging in as "cn=directory manager" with the password "secret": *  *     java CreateJavaSchema -sn41 "-ncn=directory manager" -psecret * * @author Rosanna Lee */import javax.naming.*;import javax.naming.directory.*;import java.util.Hashtable;public class CreateJavaSchema {    protected static String dn, passwd, auth;    protected static boolean netscapebug;    // NS 4.1 has problems parsing an object class definition which contains    // a MUST clause without parentheses. The workaround is to add a    // superfluous value (objectClass) to each MUST clause.    //     // It also doesn't like the Octet String syntax (use Binary instead)    //    protected static boolean netscape41bug = false;    // AD supports auxiliary classes in a peculiar way.    protected static boolean activeDirectorySchemaBug = false;    protected static boolean traceLdap = false;    protected static final int LIST = 0;    protected static final int UPDATE = 1;    private static String[] allAttrs = {	"javaSerializedObject",	"javaFactoryLocation",	"javaReferenceAddress",	"javaFactory",	"javaClassName",	"javaClassNames",	"javaDoc",	"javaSerializedData",	"javaCodebase",	"javaFactory",	"javaReferenceAddress"};    private static String[] allOCs = {	"javaObject",	"javaNamingReference",	"javaSerializedObject",	"javaRemoteObject",	"javaMarshalledObject",	"javaContainer"};    public static void main(String[] args) {	new CreateJavaSchema().run(args, allAttrs, allOCs);    }    CreateJavaSchema () {    }    protected void run(String[] args, String[] attrIDs, String[] ocIDs) {	int cmd = processCommandLine(args);	try {	    DirContext ctx = signOn();	    switch (cmd) {	    case UPDATE:		updateSchema(ctx, attrIDs, ocIDs);		break;	    default:		showSchema(ctx, attrIDs, ocIDs);	    }	} catch (NamingException e) {	    e.printStackTrace();	}    }    /**     * Signs on to directory server using parameters supplied to program.     * @return The initial context to the server.     */    private DirContext signOn() throws NamingException {	if (dn != null && auth == null) {	    auth = "simple"; 	// use simple for Netscape	}	Hashtable env = new Hashtable();	env.put(Context.INITIAL_CONTEXT_FACTORY, 	    "com.sun.jndi.ldap.LdapCtxFactory");	env.put(Context.REFERRAL, "follow");	if (auth != null) {	    env.put(Context.SECURITY_AUTHENTICATION, auth);	    env.put(Context.SECURITY_PRINCIPAL, dn);	    env.put(Context.SECURITY_CREDENTIALS, passwd);	}	// Workaround for Netscape schema bugs	if (netscapebug) {	    env.put("com.sun.naming.netscape.schemaBugs", "true");	}	// LDAP protocol tracing	if (traceLdap) {	    env.put("com.sun.jndi.ldap.trace.ber", System.err);	}	return new InitialDirContext(env);    }    void showSchema(DirContext ctx, String[] attrs, String[] ocs) 	throws NamingException {	DirContext attrRoot = 	    (DirContext)ctx.getSchema("").lookup("AttributeDefinition");	printSchema(attrRoot, attrs);    	DirContext ocRoot = 	    (DirContext)ctx.getSchema("").lookup("ClassDefinition");	printSchema(ocRoot, ocs);    }    private void printSchema(DirContext ctx, String[] ids) {	for (int i = 0; i < ids.length; i++) {	    try {		System.out.print(ids[i] + ": ");		System.out.print(ctx.getAttributes(ids[i]));	    } catch (NamingException e) {	    } finally {		System.out.println();	    }	}    }    /**     * Updates the schema:     *     * Delete obsolete attributes:     * 	javaSerializedObject     * 	javaFactoryLocation     * 	javaReferenceAddress     * 	javaFactory     * 	javaClassName     *  + all the new ones that we're going to add     * Add new and updated attributes:     * 	javaSerializedData     * 	javaCodebase     * 	javaClassName     * 	javaClassNames     *  javaFactory     *  javaReferenceAddress     *  javaDoc     *     * Delete obsolete object classes:     *  javaNamingReference     *  javaObject     *  + all the new ones that we're going to add     * Add new and updated object classes:     *  javaObject     *  javaSerializedObject     *  javaMarshalledObject     *  javaNamingReference     */    private void updateSchema(DirContext ctx, 	String[] attrIDs, String[] ocIDs) throws NamingException {	if (activeDirectorySchemaBug) {	    updateADSchema(ctx);	} else {	    updateAttributes((DirContext)		ctx.getSchema("").lookup("AttributeDefinition"), attrIDs);	    updateObjectClasses((DirContext)		ctx.getSchema("").lookup("ClassDefinition"), ocIDs);	}	System.out.println("Please use your directory server's administration tool to verify");	System.out.println("the correctness of the schema.");    }    /* Add new and updated attr definitions */    protected void updateAttributes(DirContext attrRoot, String[] attrIDs) 	throws NamingException {	/* Get rid of old attr IDs */        for (int i = 0; i < attrIDs.length; i++) {	    attrRoot.destroySubcontext(attrIDs[i]);	}// javaSerializedData	Attributes attrs = new BasicAttributes(true); // ignore case	attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.8");	attrs.put("NAME", "javaSerializedData");	attrs.put("DESC", "Serialized form of a Java object");	if (netscape41bug) {	    // DS 4.1 doesn't like Octet String	    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.5");	} else {	    attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.40");	}	attrs.put("SINGLE-VALUE", "true");	attrRoot.createSubcontext("javaSerializedData", attrs);	System.out.println("Created javaSerializedData attribute");// javaCodebase	attrs = new BasicAttributes(true);	attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.7");	attrs.put("NAME", "javaCodebase");	attrs.put("DESC", "URL(s) specifying the location of class definition");	attrs.put("EQUALITY", "caseExactIA5Match");	attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.26");	attrRoot.createSubcontext("javaCodebase", attrs);	System.out.println("Created javaCodebase attribute");// javaClassName	attrs = new BasicAttributes(true);	attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.6");	attrs.put("NAME", "javaClassName");	attrs.put("DESC", "Fully qualified name of distinguished class or interface");	attrs.put("EQUALITY", "caseExactMatch");	attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");	attrs.put("SINGLE-VALUE", "true");	attrRoot.createSubcontext("javaClassName", attrs);	System.out.println("Created javaClassName attribute");// javaClassNames	attrs = new BasicAttributes(true);	attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.1.13");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -