You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/09/26 10:34:47 UTC

svn commit: r291583 - in /directory/standalone/trunk/osgi/loader: ./ src/main/java/org/apache/ldap/server/loader/

Author: erodriguez
Date: Mon Sep 26 01:34:42 2005
New Revision: 291583

URL: http://svn.apache.org/viewcvs?rev=291583&view=rev
Log:
Updates to LDIF Loader console command.
o  Updated to use Service Binder.
o  Replaced LdapName with CompoundName to remove antlr dependency.
o  Improved exceptions and logging.
o  Replaced LdapContext with DirContext as the more appropriate interface.

Added:
    directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java   (with props)
    directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml   (with props)
Modified:
    directory/standalone/trunk/osgi/loader/project.xml
    directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
    directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java

Modified: directory/standalone/trunk/osgi/loader/project.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/loader/project.xml?rev=291583&r1=291582&r2=291583&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/loader/project.xml (original)
+++ directory/standalone/trunk/osgi/loader/project.xml Mon Sep 26 01:34:42 2005
@@ -5,7 +5,8 @@
   <name>Apache LDIF Loader</name>
   <currentVersion>0.1.1</currentVersion>
   <properties>
-    <osgi.import.package>org.apache.ldap.server.configuration</osgi.import.package>
+    <osgi.bundle.activator>org.apache.ldap.server.loader.Activator</osgi.bundle.activator>
+    <osgi.import.package>org.ungoverned.gravity.servicebinder,org.apache.ldap.server.configuration</osgi.import.package>
   </properties>
   <package>org.apache.loader</package>
   <shortDescription>Apache LDIF Loader</shortDescription>
@@ -19,26 +20,7 @@
     <dependency>
       <groupId>directory-shared</groupId>
       <artifactId>ldap-common</artifactId>
-      <version>0.9.2-SNAPSHOT</version>
-      <properties>
-        <osgi.jar.bundle>true</osgi.jar.bundle>
-      </properties>
-    </dependency>
-    <dependency>
-      <!-- bad containment of filter nodes -->
-      <groupId>directory-shared</groupId>
-      <artifactId>ldap-common</artifactId>
-      <version>0.9.2-SNAPSHOT</version>
-      <properties>
-        <osgi.jar.bundle>true</osgi.jar.bundle>
-      </properties>
-    </dependency>
-    <dependency>
-      <!-- bad containment of antlr exceptions -->
-      <groupId>antlr</groupId>
-      <artifactId>antlr</artifactId>
-      <version>2.7.2</version>
-      <url>http://antlr.org/</url>
+      <version>0.9.3-SNAPSHOT</version>
       <properties>
         <osgi.jar.bundle>true</osgi.jar.bundle>
       </properties>
@@ -47,7 +29,7 @@
       <!-- required to load Configuration base class -->
       <groupId>directory</groupId>
       <artifactId>apacheds-core</artifactId>
-      <version>0.9.2-SNAPSHOT</version>
+      <version>0.9.3-SNAPSHOT</version>
     </dependency>
   </dependencies>
 </project>

Modified: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/Activator.java?rev=291583&r1=291582&r2=291583&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/Activator.java (original)
+++ directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/Activator.java Mon Sep 26 01:34:42 2005
@@ -17,103 +17,8 @@
 
 package org.apache.ldap.server.loader;
 
-import java.io.PrintStream;
-import java.util.Hashtable;
-import java.util.Properties;
+import org.ungoverned.gravity.servicebinder.GenericActivator;
 
-import javax.naming.Context;
-import javax.naming.NamingException;
-import javax.naming.ldap.LdapContext;
-import javax.naming.spi.InitialContextFactory;
-
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceReference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.ungoverned.osgi.service.shell.Command;
-
-public class Activator implements BundleActivator, Command
+public class Activator extends GenericActivator
 {
-    /** the log for this class */
-    private static final Logger log = LoggerFactory.getLogger( Activator.class );
-
-    private LdapContext ctx;
-    private LdifFileLoader loader;
-
-    /**
-     * Implements BundleActivator.start().
-     * Logs that this service is starting and starts this service.
-     * @param context the framework context for the bundle.
-     */
-    public void start( BundleContext context ) throws BundleException
-    {
-        log.debug( "Starting Apache Load Command." );
-
-        ServiceReference storeReference = context.getServiceReference( InitialContextFactory.class.getName() );
-
-        InitialContextFactory factory = (InitialContextFactory) context.getService( storeReference );
-
-        Hashtable env = new Hashtable( new LoaderConfiguration().toJndiEnvironment() );
-        loadEnvironment( env );
-
-        try
-        {
-            ctx = (LdapContext) factory.getInitialContext( env );
-        }
-        catch ( NamingException ne )
-        {
-            throw new BundleException( "Unable to create initial context.", ne );
-        }
-
-        context.registerService( Command.class.getName(), this, new Properties() );
-    }
-
-    /**
-     * Implements BundleActivator.stop(). Logs that this service has stopped.
-     * @param context the framework context for the bundle.
-     */
-    public void stop( BundleContext context )
-    {
-        log.debug( "Stopping Apache Load Command." );
-
-        loader = null;
-    }
-
-    public String getName()
-    {
-        return "load";
-    }
-
-    public String getUsage()
-    {
-        return "load <path>";
-    }
-
-    public String getShortDescription()
-    {
-        return "Load LDIF entries into the embedded directory backing store.";
-    }
-
-    public void execute( String line, PrintStream out, PrintStream err )
-    {
-        String[] components = line.split( "\\s" );
-
-        if ( components.length > 1 )
-        {
-            loader = new LdifFileLoader( ctx, components[ 1 ] );
-            loader.execute();
-        }
-        else
-        {
-            log.debug( "Incorrect number of arguments:  load <path>" );
-        }
-    }
-
-    private void loadEnvironment( Hashtable env )
-    {
-        env.put( Context.PROVIDER_URL, "ou=system" );
-        env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
-    }
 }

Modified: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java?rev=291583&r1=291582&r2=291583&view=diff
==============================================================================
--- directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java (original)
+++ directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java Mon Sep 26 01:34:42 2005
@@ -20,13 +20,15 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
+import java.util.Properties;
 
-import javax.naming.Context;
+import javax.naming.CompoundName;
 import javax.naming.Name;
 import javax.naming.NamingException;
 import javax.naming.directory.Attributes;
-import javax.naming.ldap.LdapContext;
+import javax.naming.directory.DirContext;
 import javax.security.auth.kerberos.KerberosKey;
 import javax.security.auth.kerberos.KerberosPrincipal;
 
@@ -35,13 +37,11 @@
 import org.apache.ldap.common.ldif.LdifParser;
 import org.apache.ldap.common.ldif.LdifParserImpl;
 import org.apache.ldap.common.message.LockableAttributesImpl;
-import org.apache.ldap.common.name.LdapName;
-import org.apache.ldap.common.util.NestableRuntimeException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Command class for loading an LDIF file into an LdapContext.
+ * Command class for loading an LDIF file into an DirContext.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
@@ -52,7 +52,7 @@
     private static final Logger log = LoggerFactory.getLogger( LdifFileLoader.class );
 
     /** a handle on the top initial context: get new context from this */
-    protected LdapContext ctx;
+    protected DirContext ctx;
 
     protected String ldifPath;
 
@@ -62,7 +62,7 @@
      * @param ctx the context to load the entries into.
      * @param ldifPath the path to the file of LDIF entries.
      */
-    public LdifFileLoader( LdapContext ctx, String ldifPath )
+    public LdifFileLoader( DirContext ctx, String ldifPath )
     {
         this.ctx = ctx;
         this.ldifPath = ldifPath;
@@ -114,24 +114,32 @@
 
                 try
                 {
-                    log.error( "attempting look up before creation of " + rdn );
                     ctx.lookup( rdn );
 
-                    log.error( "succeeded on looked up of " + rdn + " will not create" );
+                    log.info( "Found " + rdn + ", will not create." );
                 }
                 catch ( Exception e )
                 {
-                    log.error( "creating " + rdn );
-
                     ctx.createSubcontext( rdn, attributes );
 
-                    log.error( "successfully created " + rdn );
+                    log.info( "Created " + rdn + "." );
                 }
             }
         }
-        catch ( Exception e )
+        catch ( FileNotFoundException fnfe )
         {
-            throw new NestableRuntimeException( "Failed to import LDIF into backing store.", e );
+            log.error( "LDIF file does not exist." );
+            return;
+        }
+        catch ( IOException ioe )
+        {
+            log.error( "Failed to import LDIF into backing store.", ioe );
+            return;
+        }
+        catch ( NamingException ne )
+        {
+            log.error( "Failed to import LDIF into backing store.", ne );
+            return;
         }
 
         try
@@ -154,15 +162,13 @@
 
                 rdn = getRelativeName( ctx, dn );
 
-                log.error( "looking up " + rdn );
-
                 Object stored = ctx.lookup( rdn );
 
-                log.error( "looked up " + rdn + " and found " + stored );
+                log.debug( "Lookup for " + rdn + " returned " + stored + "." );
 
                 if ( stored == null )
                 {
-                    log.error( rdn + " was null" );
+                    log.error( rdn + " was null." );
 
                     throw new IllegalStateException( "LDIF entries not being pushed to disk." );
                 }
@@ -170,31 +176,50 @@
         }
         catch ( Exception e )
         {
-            log.error( "Failed to find " + rdn, e );
+            log.error( "Failed to find " + rdn );
+
+            if ( log.isDebugEnabled() )
+            {
+                log.error( "Failed to import LDIF into backing store.", e );
+            }
+            else
+            {
+                log.error( "Failed to import LDIF into backing store." );
+            }
 
-            throw new NestableRuntimeException( "Failed to import LDIF into backing store.", e );
+            return;
         }
     }
 
-    private Name getRelativeName( Context base, String dn ) throws NamingException
+    private Name getRelativeName( DirContext ctx, String baseDn ) throws NamingException
     {
-        LdapName rdn = new LdapName( dn );
+        Properties props = new Properties();
+        props.setProperty( "jndi.syntax.direction", "right_to_left" );
+        props.setProperty( "jndi.syntax.separator", "," );
 
-        LdapName baseDn = new LdapName( base.getNameInNamespace() );
+        Name searchBaseDn = null;
 
-        if ( rdn.startsWith( baseDn ) )
+        try
         {
-            for ( int ii = 0; ii < baseDn.size(); ii++ )
+            Name ctxRoot = new CompoundName( ctx.getNameInNamespace(), props );
+            searchBaseDn = new CompoundName( baseDn, props );
+
+            if ( !searchBaseDn.startsWith( ctxRoot ) )
             {
-                rdn.remove( 0 );
+                throw new NamingException( "Invalid search base " + baseDn );
+            }
+
+            for ( int ii = 0; ii < ctxRoot.size(); ii++ )
+            {
+                searchBaseDn.remove( 0 );
             }
         }
-        else
+        catch ( NamingException e )
         {
-            throw new NamingException( dn + " is not a subordinate of context:" + baseDn.toString() );
+            throw new NamingException( "Failed to initialize search base " + baseDn );
         }
 
-        return rdn;
+        return searchBaseDn;
     }
 
     /**
@@ -220,7 +245,7 @@
 
             if ( in == null )
             {
-                log.error( "LDIF file does not exist!" );
+                throw new FileNotFoundException( "LDIF file does not exist." );
             }
         }
 

Added: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java?rev=291583&view=auto
==============================================================================
--- directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java (added)
+++ directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java Mon Sep 26 01:34:42 2005
@@ -0,0 +1,108 @@
+/*
+ *   Copyright 2005 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.loader;
+
+import java.io.PrintStream;
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import javax.naming.spi.InitialContextFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.ungoverned.osgi.service.shell.Command;
+
+public class LoadCommand implements Command
+{
+    /** the log for this class */
+    private static final Logger log = LoggerFactory.getLogger( LoadCommand.class );
+
+    private DirContext ctx;
+    private LdifFileLoader loader;
+    private InitialContextFactory factory;
+
+    public String getName()
+    {
+        return "load";
+    }
+
+    public String getUsage()
+    {
+        return "load <path> <context>";
+    }
+
+    public String getShortDescription()
+    {
+        return "Load LDIF entries into the embedded directory backing store.";
+    }
+
+    public void execute( String line, PrintStream out, PrintStream err )
+    {
+        String[] components = line.split( "\\s" );
+
+        int arguments = components.length - 1;
+
+        if ( arguments < 2 )
+        {
+            err.println( "Incorrect number of arguments (" + arguments + "):  load <path> <context>" );
+            return;
+        }
+
+        String pathToLdif = components[ 1 ];
+        String initialContext = components[ 2 ];
+
+        Hashtable env = new Hashtable( new LoaderConfiguration().toJndiEnvironment() );
+        env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" );
+        env.put( Context.PROVIDER_URL, initialContext );
+
+        try
+        {
+            ctx = (DirContext) factory.getInitialContext( env );
+        }
+        catch ( NamingException ne )
+        {
+            if ( log.isDebugEnabled() )
+            {
+                log.error( "Error obtaining initial context " + initialContext, ne );
+            }
+            else
+            {
+                log.error( "Error obtaining initial context " + initialContext );
+            }
+
+            return;
+        }
+
+        loader = new LdifFileLoader( ctx, pathToLdif );
+        loader.execute();
+    }
+
+    public void setInitialContextFactory( InitialContextFactory factory )
+    {
+        this.factory = factory;
+        log.debug( getName() + " has bound to " + factory );
+    }
+
+    public void unsetInitialContextFactory( InitialContextFactory factory )
+    {
+        this.factory = null;
+        log.debug( getName() + " has unbound from " + factory );
+    }
+}

Propchange: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/LoadCommand.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml
URL: http://svn.apache.org/viewcvs/directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml?rev=291583&view=auto
==============================================================================
--- directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml (added)
+++ directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml Mon Sep 26 01:34:42 2005
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--<!DOCTYPE bundle SYSTEM "metadata.dtd">-->
+<bundle>
+  <component class="org.apache.ldap.server.loader.LoadCommand">
+    <provides service="org.ungoverned.osgi.service.shell.Command"/>
+    <requires
+        service="javax.naming.spi.InitialContextFactory"
+        filter=""
+        cardinality="1..1"
+        policy="static"
+        bind-method="setInitialContextFactory"
+        unbind-method="unsetInitialContextFactory"
+    />
+  </component>
+</bundle>

Propchange: directory/standalone/trunk/osgi/loader/src/main/java/org/apache/ldap/server/loader/metadata.xml
------------------------------------------------------------------------------
    svn:eol-style = native