Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 80869 invoked from network); 30 Dec 2005 03:30:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 30 Dec 2005 03:30:09 -0000 Received: (qmail 34482 invoked by uid 500); 30 Dec 2005 03:30:08 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 34428 invoked by uid 500); 30 Dec 2005 03:30:07 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 34417 invoked by uid 99); 30 Dec 2005 03:30:07 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Dec 2005 19:30:07 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 29 Dec 2005 19:30:06 -0800 Received: (qmail 80643 invoked by uid 65534); 30 Dec 2005 03:29:46 -0000 Message-ID: <20051230032946.80642.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r359976 - in /directory/trunk: apacheds-plugin-test/ apacheds-plugin/ apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/ apacheds-plugin/src/main/resources/ apacheds-plugin/src/main/resources/org/ apacheds-plugin/src/main/re... Date: Fri, 30 Dec 2005 03:29:45 -0000 To: commits@directory.apache.org From: akarasulu@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Thu Dec 29 19:29:35 2005 New Revision: 359976 URL: http://svn.apache.org/viewcvs?rev=359976&view=rev Log: finishing up m2 plugin Added: directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.java directory/trunk/apacheds-plugin/src/main/resources/ directory/trunk/apacheds-plugin/src/main/resources/org/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/schema/ directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/schema/AttributeTypes.template - copied unchanged from r359946, directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/AttributeTypes.template directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/schema/ObjectClasses.template - copied unchanged from r359946, directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/ObjectClasses.template directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/schema/Schema.template - copied unchanged from r359946, directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.template directory/trunk/apacheds-plugin/src/main/resources/org/apache/ldap/server/tools/schema/typeless.template - copied unchanged from r359946, directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/typeless.template Removed: directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/AttributeTypes.template directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/ObjectClasses.template directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.template directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/typeless.template Modified: directory/trunk/apacheds-plugin-test/pom.xml directory/trunk/apacheds-plugin/pom.xml directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/DirectorySchemaToolMojo.java Modified: directory/trunk/apacheds-plugin-test/pom.xml URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds-plugin-test/pom.xml?rev=359976&r1=359975&r2=359976&view=diff ============================================================================== --- directory/trunk/apacheds-plugin-test/pom.xml (original) +++ directory/trunk/apacheds-plugin-test/pom.xml Thu Dec 29 19:29:35 2005 @@ -6,35 +6,15 @@ build 0.9.4-SNAPSHOT - org.apache.ldap.server.plugin - apacheds-plugin - maven-plugin + org.apache.ldap.server.plugintest + apacheds-plugin-test + jar - org.apache.maven - maven-plugin-api - 2.0 - - - ant - ant - 1.5.3-1 - - - antlr - antlr - 2.7.2 - - org.apache.ldap.server - org.apache.ldap.server.shared + org.apache.ldap.server.plugin 0.9.4-SNAPSHOT - - velocity - velocity-dep - 1.4 - @@ -44,6 +24,30 @@ org.apache.ldap.server.plugin src/main/schema + + + core + uid=testuser + + system + + + + java + uid=testuser2 + + system + core + + + + inetorgperson + + system + core + + + @@ -53,21 +57,6 @@ generate - - - - - org.apache.maven.plugins - maven-antlr-plugin - - openldap.g - - - - - generate - - Modified: directory/trunk/apacheds-plugin/pom.xml URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds-plugin/pom.xml?rev=359976&r1=359975&r2=359976&view=diff ============================================================================== --- directory/trunk/apacheds-plugin/pom.xml (original) +++ directory/trunk/apacheds-plugin/pom.xml Thu Dec 29 19:29:35 2005 @@ -40,23 +40,6 @@ - org.apache.ldap.server - org.apache.ldap.server.plugin - - src/main/schema - - - - - compile - - generate - - - - - - org.apache.maven.plugins maven-antlr-plugin Modified: directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/DirectorySchemaToolMojo.java URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/DirectorySchemaToolMojo.java?rev=359976&r1=359975&r2=359976&view=diff ============================================================================== --- directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/DirectorySchemaToolMojo.java (original) +++ directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/DirectorySchemaToolMojo.java Thu Dec 29 19:29:35 2005 @@ -18,17 +18,30 @@ import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; +import org.apache.ldap.server.schema.bootstrap.AbstractBootstrapSchema; +import org.apache.ldap.server.schema.bootstrap.BootstrapSchema; +import org.apache.ldap.server.schema.bootstrap.ProducerTypeEnum; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; /** * Maven 2 plugin mojo wrapper for directory plugin. * - * @goal sayhi - * @description Says "Hi" to the user + * @goal generate + * @description Generates ApacheDS schema classes from OpenLDAP schema files * @author Apache Directory Project * @version $Rev$ */ @@ -36,19 +49,383 @@ { /** * The directory containing the OpenLDAP schema files. + * @parameter expression="src/main/schema" */ - private File schemaSourcesDir; + private File sourceDirectory; + /** + * The target directory into which the plugin generates schema java sources. + * @parameter expression="target/generated-sources" + */ + private File outputDirectory; + + /** + * The default package to use for generated schema classes. + * @parameter expression="org.apache.ldap.server.schema.bootstrap" + */ + private String defaultPackage; + + /** + * The distinguished name of the default schema owner. + * @parameter expression="uid=admin,ou=system" + */ + private String defaultOwner; + + /** + * The set of schemas to generate classes for. + * @parameter + */ + private Schema[] schemas; + + /** + * Toggles verbose output. + * @parameter expression="true" + */ + private boolean verboseOutput; + + // Need to figure this out from the pom - maybe make it a parameter for mojoj and set expression for pom element + private String javaSrcDir = "src/main/java"; + + + public DirectorySchemaToolMojo() throws Exception + { + Velocity.init(); + } + + + private void generate( Schema schema ) throws Exception + { + BootstrapSchema bootstrapSchema = new AbstractBootstrapSchema( schema.getOwner(), + schema.getName(), schema.getPkg(), schema.getDependencies() ){}; + + if ( schema == null ) + { + throw new NullPointerException( "the schema property must be set" ); + } + + String filePath = sourceDirectory + File.separator + schema.getName() + ".schema"; + InputStream in = new FileInputStream( filePath ); + OpenLdapSchemaParser parser = new OpenLdapSchemaParser(); + parser.parse( in ); + generateSchema( bootstrapSchema ); + generateAttributeTypes( parser, bootstrapSchema ); + generateObjectClasses( parser, bootstrapSchema ); + generateRest( bootstrapSchema ); + } + + + protected void generateSchema( BootstrapSchema schema ) throws Exception + { + StringBuffer schemaCapped = new StringBuffer(); + schemaCapped.append( Character.toUpperCase( schema.getSchemaName().charAt( 0 ) ) ); + schemaCapped.append( schema.getSchemaName().substring( 1, schema.getSchemaName().length() ) ); + + VelocityContext context = new VelocityContext(); + context.put( "package", schema.getPackageName() ); + context.put( "classname", schemaCapped.toString() + "Schema" ); + context.put( "schema", schema.getSchemaName() ); + context.put( "owner", schema.getOwner() ) ; + context.put( "deps", schema.getDependencies() ) ; + + Reader fileIn = getResourceReader( "Schema.template" ); + Writer writer = getResourceWriter( schema.getPackageName(), schema.getUnqualifiedClassName() ); + Velocity.evaluate( context, writer, "LOG", fileIn ); + + writer.flush(); + writer.close(); + } + + + protected void generateRest( BootstrapSchema schema ) throws Exception + { + List types = new ArrayList(); + types.addAll( ProducerTypeEnum.list() ); + types.remove( ProducerTypeEnum.ATTRIBUTE_TYPE_PRODUCER ); + types.remove( ProducerTypeEnum.OBJECT_CLASS_PRODUCER ); + + ProducerTypeEnum type = null; + for ( int ii = 0; ii < types.size(); ii++ ) + { + type = ( ProducerTypeEnum ) types.get( ii ); + + if ( exists( schema.getFullDefaultBaseClassName( type ), type ) ) + { + continue; + } + + VelocityContext context = new VelocityContext(); + context.put( "package", schema.getPackageName() ); + context.put( "classname", schema.getUnqualifiedClassName( type ) ); + context.put( "schema", schema.getSchemaName() ); + context.put( "owner", schema.getOwner() ) ; + context.put( "type", type.getName().substring( 0, type.getName().length() - 8 ) ) ; + + String typeName = null; + switch( type.getValue() ) + { + case( ProducerTypeEnum.COMPARATOR_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.COMPARATOR_PRODUCER"; + break; + case( ProducerTypeEnum.DIT_CONTENT_RULE_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.DIT_CONTENT_RULE_PRODUCER"; + break; + case( ProducerTypeEnum.DIT_STRUCTURE_RULE_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.DIT_STRUCTURE_RULE_PRODUCER"; + break; + case( ProducerTypeEnum.MATCHING_RULE_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.MATCHING_RULE_PRODUCER"; + break; + case( ProducerTypeEnum.MATCHING_RULE_USE_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.MATCHING_RULE_USE_PRODUCER"; + break; + case( ProducerTypeEnum.NAME_FORM_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.NAME_FORM_PRODUCER"; + break; + case( ProducerTypeEnum.NORMALIZER_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.NORMALIZER_PRODUCER"; + break; + case( ProducerTypeEnum.SYNTAX_CHECKER_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.SYNTAX_CHECKER_PRODUCER"; + break; + case( ProducerTypeEnum.SYNTAX_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.SYNTAX_PRODUCER"; + break; + case( ProducerTypeEnum.STATE_FACTORY_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.STATE_FACTORY_PRODUCER"; + break; + case( ProducerTypeEnum.OBJECT_FACTORY_PRODUCER_VAL ): + typeName = "ProducerTypeEnum.OBJECT_FACTORY_PRODUCER"; + break; + default: + throw new IllegalStateException( "Unexpected producer: " + type.getName() ); + } + + context.put( "typeName", typeName ) ; + runVelocity( schema.getPackageName(), schema.getUnqualifiedClassName( type ), + context, "typeless.template", type ); + } + } + + + protected void generateAttributeTypes( OpenLdapSchemaParser parser, BootstrapSchema schema ) throws Exception + { + final ProducerTypeEnum type = ProducerTypeEnum.ATTRIBUTE_TYPE_PRODUCER; + + // check to see if the producer exists for this type + if ( exists( schema.getFullDefaultBaseClassName( type ), type ) ) + { + return; + } + + int size = parser.getAttributeTypes().size(); + AttributeTypeLiteral[] attributeTypes = new AttributeTypeLiteral[size]; + attributeTypes = ( AttributeTypeLiteral[] ) parser.getAttributeTypes().values().toArray( attributeTypes ); + VelocityContext context = new VelocityContext(); + context.put( "package", schema.getPackageName() ); + context.put( "classname", schema.getUnqualifiedClassName( type ) ); + context.put( "schema", schema.getSchemaName() ); + context.put( "owner", schema.getOwner() ) ; + context.put( "schemaDepCount", new Integer( schema.getDependencies().length ) ); + context.put( "schemaDeps", new String[] { "dep1", "dep2" } ) ; + context.put( "attrTypes", attributeTypes ); + runVelocity( schema.getPackageName(), schema.getUnqualifiedClassName( type ), + context, "AttributeTypes.template", type ); + } + + + protected void generateObjectClasses( OpenLdapSchemaParser parser, BootstrapSchema schema ) throws Exception + { + final ProducerTypeEnum type = ProducerTypeEnum.OBJECT_CLASS_PRODUCER; + + // check to see if the producer exists for this type + if ( exists( schema.getFullDefaultBaseClassName( type ), type ) ) + { + return; + } + + int size = parser.getObjectClassTypes().size(); + ObjectClassLiteral[] objectClasses = new ObjectClassLiteral[size]; + objectClasses = ( ObjectClassLiteral[] ) parser.getObjectClassTypes().values().toArray( objectClasses ); + + VelocityContext context = new VelocityContext(); + context.put( "package", schema.getPackageName() ); + context.put( "classname", schema.getUnqualifiedClassName( type ) ); + context.put( "schema", schema.getSchemaName() ); + context.put( "owner", schema.getOwner() ) ; + context.put( "schemaDepCount", new Integer( schema.getDependencies().length ) ); + context.put( "schemaDeps", new String[] { "dep1", "dep2" } ) ; + context.put( "objectClasses", objectClasses ); + runVelocity( schema.getPackageName(), schema.getUnqualifiedClassName( type ), + context, "ObjectClasses.template", type ); + } + + + + protected void runVelocity( String pkg, String uqcn, VelocityContext context, String template, ProducerTypeEnum type ) + throws Exception + { + Reader fileIn = getResourceReader( template ); + Writer writer = getResourceWriter( pkg, uqcn ); + Velocity.evaluate( context, writer, "LOG", fileIn ); + writer.flush(); + writer.close(); + } + + + protected Reader getResourceReader( String res ) throws IOException + { + return new InputStreamReader( getClass().getResourceAsStream( res ) ); + } + + + protected boolean mkdirs( String base, String path ) + { + String[] comps = path.split( "/" ); + File file = new File( base ); + + if ( ! file.exists() ) + { + file.mkdirs(); + } + + for ( int ii = 0; ii < comps.length; ii++ ) + { + file = new File( file, comps[ii] ); + + if ( ! file.exists() ) + { + file.mkdirs(); + } + } + + return file.exists(); + } + + + protected FileWriter getResourceWriter( String pkg, String classname ) throws IOException + { + mkdirs( outputDirectory.getPath(), pkg.replace( '.', File.separatorChar ) ); + File base = outputDirectory; + String relativePath = pkg.replace( '.', File.separatorChar ); + File dir = new File( base, relativePath ); + return new FileWriter( new File( dir, classname + ".java" ) ); + } + + + protected boolean exists( String defaultClass, ProducerTypeEnum type ) + { + // check to see if any of the classes are available in the java + // source directory, if so we return true + File defaultFile = new File( javaSrcDir + File.separator + getFilePath( defaultClass ) ); + return defaultFile.exists(); + } + + + private String getFilePath( String fqcn ) + { + String path = fqcn.replace( '.', File.separatorChar ); + path += ".java"; + return path; + } + + public void execute() throws MojoExecutionException { - try + // Bypass if no schemas have yet been defined + if ( schemas == null || schemas.length == 0 ) + { + getLog().warn( "No schemas defined for directory plugin!" ); + return; + } + + // Make sure schema configurations have a name field and set defaults + // for any other missing properties of the bean: pkg and owner. + for ( int ii = 0; ii < schemas.length; ii++ ) { - getLog().info( "schemaSourcesDir = " + schemaSourcesDir.getCanonicalPath() ); + Schema schema = schemas[ii]; + + if ( schema.getName() == null ) + { + String msg = ii + "th schema configuration element must specify a name."; + getLog().error( msg ); + throw new MojoExecutionException( msg ); + } + + if ( schema.getPkg() == null ) + { + schema.setPkg( defaultPackage ); + } + + if ( schema.getOwner() == null ) + { + schema.setOwner( defaultOwner ); + } + } + + // Report configuration if verbose output is enabled + if ( verboseOutput ) + { + report(); } - catch ( IOException e ) + + // Create output directory if it does not exist + if ( ! outputDirectory.exists() ) + { + outputDirectory.mkdirs(); + } + + // Generate for each schema + for ( int ii = 0; ii < schemas.length; ii++ ) + { + try + { + generate( schemas[ii] ); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "Failed while generating sources for " + schemas[ii].getName(), e ); + } + } + } + + private void report() + { + getLog().info( "===================================================================" ); + getLog().info( "[directory:generate]" ); + getLog().info( "sourceDirectory = " + sourceDirectory ); + getLog().info( "outputDirectory = " + outputDirectory ); + getLog().info( "defaultPackage = " + defaultPackage ); + getLog().info( "defaultOwner = " + defaultOwner ); + getLog().info( "----------------------------- schemas -----------------------------" ); + + if ( schemas != null ) { - e.printStackTrace(); + for ( int ii = 0; ii < schemas.length; ii++ ) + { + getLog().info( "SCHEMA: " + schemas[ii].getName() ); + + if ( schemas[ii].getDependencies() != null ) + { + StringBuffer buf = new StringBuffer(); + for ( int jj = 0; jj < schemas[ii].getDependencies().length; jj++ ) + { + buf.append( schemas[ii].getDependencies()[jj] ); + buf.append( " " ); + } + getLog().info( "DEPENDENCIES: " + buf.toString() ); + } + + getLog().info( "PACKAGE: " + schemas[ii].getPkg() ); + getLog().info( "OWNER: " + schemas[ii].getOwner() ); + + if ( ii + 1 < schemas.length ) + { + getLog().info( "" ); + } + } } + + getLog().info( "===================================================================" ); } } Added: directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.java URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.java?rev=359976&view=auto ============================================================================== --- directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.java (added) +++ directory/trunk/apacheds-plugin/src/main/java/org/apache/ldap/server/tools/schema/Schema.java Thu Dec 29 19:29:35 2005 @@ -0,0 +1,74 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ldap.server.tools.schema; + + +public class Schema +{ + private String name; + private String[] dependencies; + private String pkg; + private String owner; + + + public void setDependencies( String[] dependencies ) + { + this.dependencies = dependencies; + } + + + public String[] getDependencies() + { + return dependencies; + } + + + public void setPkg( String pkg ) + { + this.pkg = pkg; + } + + + public String getPkg() + { + return pkg; + } + + + public void setOwner( String owner ) + { + this.owner = owner; + } + + + public String getOwner() + { + return owner; + } + + + public void setName( String name ) + { + this.name = name; + } + + + public String getName() + { + return name; + } +}