directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r178687 - in /directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org: apache/ldap/server/loader/Activator.java apache/ldap/server/loader/LdifFileLoader.java ungoverned/ ungoverned/osgi/ ungoverned/osgi/service/ ungoverned/osgi/service/shell/ ungoverned/osgi/service/shell/Command.java
Date Thu, 26 May 2005 20:59:07 GMT
Author: erodriguez
Date: Thu May 26 13:59:07 2005
New Revision: 178687

URL: http://svn.apache.org/viewcvs?rev=178687&view=rev
Log:
Console command for populating the ApacheDS DIT from an LDIF file.

Added:
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/
    directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/Command.java

Added: directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/Activator.java?rev=178687&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
(added)
+++ directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/Activator.java
Thu May 26 13:59:07 2005
@@ -0,0 +1,116 @@
+/*
+ *   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 java.util.Properties;
+
+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.ungoverned.osgi.service.shell.Command;
+
+public class Activator implements BundleActivator, Command
+{
+    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
+    {
+        System.out.println( "Starting Apache Load Command." );
+        
+        ServiceReference storeReference = context.getServiceReference( InitialContextFactory.class.getName()
);
+        
+        InitialContextFactory factory = (InitialContextFactory) context.getService( storeReference
);
+        
+        Hashtable env = new Hashtable();
+        loadEnvironment( env );
+        
+        try
+        {
+            ctx = ( LdapContext ) factory.getInitialContext( env );
+        }
+        catch (NamingException ne)
+        {
+            throw new BundleException( "Problem loading initial context.  Failing." );
+        }
+        
+        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 )
+    {
+        System.out.println( "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
+        {
+            System.out.println( "Incorrect number of arguments:  load <path>" );
+        }
+    }
+    
+    private void loadEnvironment( Hashtable env )
+    {
+        env.put( "java.naming.provider.url", "dc=example,dc=com" );
+        env.put( "java.naming.security.principal", "uid=admin,ou=system" );
+        env.put( "java.naming.security.authentication", "simple" );
+        env.put( "java.naming.security.credentials", "secret" );
+        env.put( "java.naming.factory.initial", "org.apache.ldap.server.jndi.CoreContextFactory"
);
+    }
+}

Added: directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java?rev=178687&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java
(added)
+++ directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/apache/ldap/server/loader/LdifFileLoader.java
Thu May 26 13:59:07 2005
@@ -0,0 +1,232 @@
+/*
+ *   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.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.ldap.LdapContext;
+import javax.security.auth.kerberos.KerberosKey;
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.kerberos.store.KerberosAttribute;
+import org.apache.ldap.common.ldif.LdifIterator;
+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;
+
+/**
+ * Command class for loading an LDIF file into an LdapContext.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class LdifFileLoader
+{
+    /** a handle on the top initial context: get new context from this */
+    protected LdapContext ctx;
+    
+    protected String ldifPath;
+
+    /**
+     * Creates the LDIF file loader command.
+     *
+     * @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 )
+    {
+        this.ctx      = ctx;
+        this.ldifPath = ldifPath;
+    }
+    
+    /**
+     * Opens the LDIF file and loads the entries into the context.
+     */
+    public void execute()
+    {
+        Name rdn = null;
+
+        try
+        {
+            InputStream in = getLdifStream();
+
+            LdifIterator iterator = new LdifIterator( in );
+
+            LdifParser ldifParser = new LdifParserImpl();
+
+            while ( iterator.hasNext() )
+            {
+                String ldif = ( String ) iterator.next();
+
+                Attributes attributes = new LockableAttributesImpl();
+
+                ldifParser.parse( attributes, ldif );
+
+                String dn = ( String ) attributes.remove( "dn" ).get();
+
+                if ( attributes.get( "objectClass" ).contains( "krb5KDCEntry" ) )
+                {
+                    String pw = ( String ) attributes.get( "userpassword" ).get();
+
+                    String krbPrincipal = ( String ) attributes.get( KerberosAttribute.PRINCIPAL
).get();
+
+                    KerberosPrincipal principal = new KerberosPrincipal( krbPrincipal );
+
+                    KerberosKey key = new KerberosKey( principal, pw.toCharArray(), "DES"
) ;
+
+                    byte[] encodedKey = key.getEncoded();
+
+                    attributes.put( KerberosAttribute.KEY, encodedKey );
+
+                    attributes.put( KerberosAttribute.VERSION, Integer.toString( key.getVersionNumber()
) );
+
+                    attributes.put( KerberosAttribute.TYPE, Integer.toString( key.getKeyType()
) );
+                }
+
+                rdn = getRelativeName( ctx, dn );
+
+                try
+                {
+                    System.err.println( "attempting look up before creation of " + rdn );
+                    ctx.lookup( rdn );
+
+                    System.err.println( "succeeded on looked up of " + rdn + " will not create"
);
+                }
+                catch( Exception e )
+                {
+                    System.err.println( "creating " + rdn );
+
+                    ctx.createSubcontext( rdn, attributes );
+
+                    System.err.println( "successfully created " + rdn );
+                }
+            }
+        }
+        catch( Exception e )
+        {
+            String msg = "Failed to import LDIF into backing store.";
+            
+            throw new NestableRuntimeException( msg, e );
+        }
+
+        try
+        {
+            InputStream in = getLdifStream();
+
+            LdifIterator iterator = new LdifIterator( in );
+
+            LdifParser ldifParser = new LdifParserImpl();
+
+            while ( iterator.hasNext() )
+            {
+                String ldif = ( String ) iterator.next();
+
+                Attributes attributes = new LockableAttributesImpl();
+
+                ldifParser.parse( attributes, ldif );
+
+                String dn = ( String ) attributes.remove( "dn" ).get();
+
+                rdn = getRelativeName( ctx, dn );
+
+                System.err.println( "looking up " + rdn );
+
+                Object stored = ctx.lookup( rdn );
+
+                System.err.println( "looked up " + rdn + " and found " + stored  );
+
+                if ( stored == null )
+                {
+                    System.err.println( rdn + " was null" );
+
+                    throw new IllegalStateException( "LDIF entries not being pushed to disk."
);
+                }
+            }
+        }
+        catch( Exception e )
+        {
+            e.printStackTrace();
+
+            System.err.println( "failed to find " + rdn );
+
+            String msg = "Failed to import LDIF into backing store.";
+            
+            throw new NestableRuntimeException( msg, e );
+        }
+    }
+
+    private Name getRelativeName( Context base, String dn ) throws NamingException
+    {
+        LdapName rdn = new LdapName( dn );
+
+        LdapName baseDn = new LdapName( base.getNameInNamespace() );
+
+        if ( rdn.startsWith( baseDn ) )
+        {
+            for ( int ii = 0; ii < baseDn.size(); ii++ )
+            {
+                rdn.remove( 0 );
+            }
+        }
+        else
+        {
+            throw new NamingException( dn + " is not a subordinate of context:" + baseDn.toString()
);
+        }
+
+        return rdn;
+    }
+    
+    /**
+     * Tries to find an LDIF file either on the file system or packaged within a jar.
+     *
+     * @return the input stream to the ldif file.
+     * @throws FileNotFoundException if the file cannot be found.
+     */
+    private InputStream getLdifStream() throws FileNotFoundException
+    {
+        File file = new File( ldifPath );
+
+        InputStream in = null;
+
+        if ( file.exists() )
+        {
+            in = new FileInputStream( file );
+        }
+        else
+        {
+            // if file not on system see if something is bundled with the jar ...
+            in = getClass().getResourceAsStream( ldifPath );
+
+            if ( in == null )
+            {
+                System.err.println( "LDIF file does not exist!" );
+            }
+        }
+
+        return in;
+    }
+}

Added: directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/Command.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/Command.java?rev=178687&view=auto
==============================================================================
--- directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/Command.java
(added)
+++ directory/sandbox/trunk/osgi-core/trunk/loader/src/main/java/org/ungoverned/osgi/service/shell/Command.java
Thu May 26 13:59:07 2005
@@ -0,0 +1,87 @@
+/*
+ * Oscar - An implementation of the OSGi framework.
+ * Copyright (c) 2004, Richard S. Hall
+ * 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 the ungoverned.org 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.
+ * 
+ * Contact: Richard S. Hall (heavy@ungoverned.org)
+ * Contributor(s):
+ *
+**/
+package org.ungoverned.osgi.service.shell;
+
+import java.io.PrintStream;
+
+/**
+ * This interface is used to define commands for the Oscar shell
+ * service. Any bundle wishing to create commands for the Oscar
+ * shell service simply needs to create a service object that
+ * implements this interface and then register it with the OSGi
+ * framework. The Oscar shell service automatically includes any
+ * registered command services in its list of available commands.
+**/
+public interface Command
+{
+    /**
+     * Returns the name of the command that is implemented by the
+     * interface. The command name should not contain whitespace
+     * and should also be unique.
+     * @return the name of the command.
+    **/
+    public String getName();
+
+    /**
+     * Returns the usage string for the command. The usage string is
+     * a short string that illustrates how to use the command on the
+     * command line. This information is used when generating command
+     * help information. An example usage string for the <tt>install</tt>
+     * command is:
+     * <pre>
+     *     install <URL> [<URL> ...]
+     * </pre>
+     * @return the usage string for the command.
+    **/
+    public String getUsage();
+
+    /**
+     * Returns a short description of the command; this description
+     * should be as short as possible. This information is used when
+     * generating the command help information.
+     * @return a short description of the command.
+    **/
+    public String getShortDescription();
+
+    /**
+     * Executes the command using the supplied command line, output
+     * print stream, and error print stream.
+     * @param line the complete command line, including the command name.
+     * @param out the print stream to use for standard output.
+     * @param err the print stream to use for standard error.
+    **/
+    public void execute(String line, PrintStream out, PrintStream err);
+}



Mime
View raw message