directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r981907 - in /directory/studio/trunk: ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ ldapservers/ ldapservers/src/main/java/org/apache/directory/studio/ldapservers/
Date Tue, 03 Aug 2010 14:59:43 GMT
Author: pamarcelot
Date: Tue Aug  3 14:59:43 2010
New Revision: 981907

URL: http://svn.apache.org/viewvc?rev=981907&view=rev
Log:
Moved more methods (common to all Apache DS Server Adapters) to the LDAP Servers plugin.

Modified:
    directory/studio/trunk/ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ApacheDS156LdapServerAdapter.java
    directory/studio/trunk/ldapservers/pom.xml
    directory/studio/trunk/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java

Modified: directory/studio/trunk/ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ApacheDS156LdapServerAdapter.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ApacheDS156LdapServerAdapter.java?rev=981907&r1=981906&r2=981907&view=diff
==============================================================================
--- directory/studio/trunk/ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ApacheDS156LdapServerAdapter.java
(original)
+++ directory/studio/trunk/ldapservers.apacheds.v156/src/main/java/org/apache/directory/studio/ldapserver/apacheds/v156/ApacheDS156LdapServerAdapter.java
Tue Aug  3 14:59:43 2010
@@ -24,44 +24,22 @@ package org.apache.directory.studio.ldap
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
 import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIOException;
 import org.apache.directory.studio.apacheds.configuration.model.v156.ServerConfigurationV156;
 import org.apache.directory.studio.apacheds.configuration.model.v156.ServerXmlIOV156;
 import org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
-import org.apache.directory.studio.common.ui.CommonUiUtils;
 import org.apache.directory.studio.ldapservers.LdapServersManager;
 import org.apache.directory.studio.ldapservers.LdapServersUtils;
 import org.apache.directory.studio.ldapservers.model.LdapServer;
 import org.apache.directory.studio.ldapservers.model.LdapServerAdapter;
-import org.apache.directory.studio.ldapservers.model.LdapServerStatus;
-import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
-import org.eclipse.debug.core.DebugEvent;
-import org.eclipse.debug.core.DebugPlugin;
-import org.eclipse.debug.core.IDebugEventSetListener;
 import org.eclipse.debug.core.ILaunch;
-import org.eclipse.debug.core.ILaunchConfiguration;
-import org.eclipse.debug.core.ILaunchConfigurationType;
-import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
-import org.eclipse.debug.core.ILaunchManager;
-import org.eclipse.debug.core.model.RuntimeProcess;
-import org.eclipse.debug.ui.IDebugUIConstants;
-import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
-import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
-import org.eclipse.jdt.launching.IVMInstall;
-import org.eclipse.jdt.launching.JavaRuntime;
-import org.eclipse.osgi.util.NLS;
+import org.osgi.framework.Bundle;
 
 
 /**
@@ -71,9 +49,6 @@ import org.eclipse.osgi.util.NLS;
  */
 public class ApacheDS156LdapServerAdapter implements LdapServerAdapter
 {
-    /** The ID of the launch configuration custom object */
-    private static final String LAUNCH_CONFIGURATION_CUSTOM_OBJECT = "launchConfiguration";
-
     // Various strings constants used in paths
     private static final String SERVER_XML = "server.xml";
     private static final String LOG4J_PROPERTIES = "log4j.properties";
@@ -113,9 +88,13 @@ public class ApacheDS156LdapServerAdapte
      */
     public void add( LdapServer server, StudioProgressMonitor monitor ) throws Exception
     {
+        // Getting the bundle associated with the plugin
+        Bundle bundle = ApacheDS156Plugin.getDefault().getBundle();
+
         // Verifying and copying ApacheDS 1.5.6 libraries
         monitor.subTask( "verifying and copying ApacheDS 1.5.6 libraries" );
-        verifyAndCopyLibraries();
+        LdapServersUtils.verifyAndCopyLibraries( bundle, new Path( RESOURCES ).append( LIBS
),
+            getServerLibrariesFolder(), libraries );
 
         // Creating server folder structure
         monitor.subTask( "creating server folder structure" );
@@ -132,8 +111,10 @@ public class ApacheDS156LdapServerAdapte
         // Copying configuration files
         monitor.subTask( "copying configuration files" );
         IPath resourceConfFolderPath = new Path( RESOURCES ).append( CONF );
-        copyResource( resourceConfFolderPath.append( SERVER_XML ), new File( confFolder,
SERVER_XML ) );
-        copyResource( resourceConfFolderPath.append( LOG4J_PROPERTIES ), new File( confFolder,
LOG4J_PROPERTIES ) );
+        LdapServersUtils.copyResource( bundle, resourceConfFolderPath.append( SERVER_XML
), new File( confFolder,
+            SERVER_XML ) );
+        LdapServersUtils.copyResource( bundle, resourceConfFolderPath.append( LOG4J_PROPERTIES
), new File( confFolder,
+            LOG4J_PROPERTIES ) );
     }
 
 
@@ -142,7 +123,8 @@ public class ApacheDS156LdapServerAdapte
      */
     public void delete( LdapServer server ) throws Exception
     {
-        System.out.println( "delete " + server.getName() );
+        // Nothing to do (nothing more than the default behavior of 
+        // the delete action before this method is called)
     }
 
 
@@ -152,10 +134,10 @@ public class ApacheDS156LdapServerAdapte
     public void start( LdapServer server, StudioProgressMonitor monitor ) throws Exception
     {
         // Launching Apache DS
-        ILaunch launch = launchApacheDS( server );
+        ILaunch launch = LdapServersUtils.launchApacheDS( server, getServerLibrariesFolder(),
libraries );
 
         // Starting the "terminate" listener thread
-        startTerminateListenerThread( server, launch );
+        LdapServersUtils.startTerminateListenerThread( server, launch );
 
         // Running the startup listener watchdog
         LdapServersUtils.runStartupListenerWatchdog( server, getTestingPort( server ) );
@@ -163,99 +145,12 @@ public class ApacheDS156LdapServerAdapte
 
 
     /**
-     * Launches Apache DS using a launch configuration.
-     *
-     * @param server
-     *      the server
-     * @return
-     *      the associated launch
-     */
-    private ILaunch launchApacheDS( LdapServer server ) throws Exception
-    {
-        // Getting the default VM installation
-        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
-
-        // Creating a new editable launch configuration
-        ILaunchConfigurationType type = DebugPlugin.getDefault().getLaunchManager()
-            .getLaunchConfigurationType( IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION
);
-        ILaunchConfigurationWorkingCopy workingCopy = type.newInstance( null, NLS.bind( "Starting
{0}", new String[]
-            { server.getName() } ) );
-
-        // Setting the JRE container path attribute
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH,
vmInstall
-            .getInstallLocation().toString() );
-
-        // Setting the main type attribute
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
-            "org.apache.directory.studio.apacheds.Launcher" ); //$NON-NLS-1$
-
-        // Creating the classpath list
-        List<String> classpath = new ArrayList<String>();
-        IPath apacheDsLibrariesFolder = getServerLibrariesFolder();
-        for ( String library : libraries )
-        {
-            IRuntimeClasspathEntry libraryClasspathEntry = JavaRuntime
-                .newArchiveRuntimeClasspathEntry( apacheDsLibrariesFolder.append( library
) );
-            libraryClasspathEntry.setClasspathProperty( IRuntimeClasspathEntry.USER_CLASSES
);
-
-            classpath.add( libraryClasspathEntry.getMemento() );
-        }
-
-        // Setting the classpath type attribute
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath
);
-
-        // Setting the default classpath type attribute to false
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,
false );
-
-        // The server folder path
-        IPath serverFolderPath = LdapServersManager.getServerFolder( server );
-
-        // Setting the program arguments attribute
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
"\"" //$NON-NLS-1$
-            + serverFolderPath.toOSString() + "\"" ); //$NON-NLS-1$
-
-        // Creating the VM arguments string
-        StringBuffer vmArguments = new StringBuffer();
-        vmArguments.append( "-Dlog4j.configuration=file:\"" //$NON-NLS-1$
-            + serverFolderPath.append( "conf" ).append( "log4j.properties" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-        vmArguments.append( " " ); //$NON-NLS-1$
-        vmArguments.append( "-Dapacheds.var.dir=\"" + serverFolderPath.toOSString() + "\""
); //$NON-NLS-1$ //$NON-NLS-2$
-        vmArguments.append( " " ); //$NON-NLS-1$
-        vmArguments.append( "-Dapacheds.log.dir=\"" + serverFolderPath.append( "log" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-        vmArguments.append( " " ); //$NON-NLS-1$
-        vmArguments.append( "-Dapacheds.run.dir=\"" + serverFolderPath.append( "run" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-        vmArguments.append( " " ); //$NON-NLS-1$
-        vmArguments.append( "-Dapacheds.instance=\"" + server.getName() + "\"" ); //$NON-NLS-1$
//$NON-NLS-2$
-
-        // Setting the VM arguments attribute
-        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArguments.toString()
);
-
-        // Setting the launch configuration as private
-        workingCopy.setAttribute( IDebugUIConstants.ATTR_PRIVATE, true );
-
-        // Indicating that we don't want any console to show up
-        workingCopy.setAttribute( DebugPlugin.ATTR_CAPTURE_OUTPUT, false );
-
-        // Saving the launch configuration
-        ILaunchConfiguration configuration = workingCopy.doSave();
-
-        // Launching the launch configuration
-        ILaunch launch = configuration.launch( ILaunchManager.RUN_MODE, new NullProgressMonitor()
);
-
-        // Storing the launch configuration as a custom object in the LDAP Server for later
use
-        server.putCustomObject( LAUNCH_CONFIGURATION_CUSTOM_OBJECT, launch );
-
-        return launch;
-    }
-
-
-    /**
      * {@inheritDoc}
      */
     public void stop( LdapServer server, StudioProgressMonitor monitor ) throws Exception
     {
         // Getting the launch
-        ILaunch launch = ( ILaunch ) server.removeCustomObject( LAUNCH_CONFIGURATION_CUSTOM_OBJECT
);
+        ILaunch launch = ( ILaunch ) server.removeCustomObject( LdapServersUtils.LAUNCH_CONFIGURATION_CUSTOM_OBJECT
);
         if ( ( launch != null ) && ( !launch.isTerminated() ) )
         {
             // Terminating the launch
@@ -269,43 +164,6 @@ public class ApacheDS156LdapServerAdapte
 
 
     /**
-     * Verifies that the libraries folder exists and contains the jar files 
-     * needed to launch the server.
-     */
-    public static void verifyAndCopyLibraries()
-    {
-        // Source libraries folder
-        IPath sourceLibrariesPath = new Path( RESOURCES ).append( LIBS );
-
-        // Destination libraries folder
-        IPath destinationLibrariesFolderPath = getServerLibrariesFolder();
-        File destinationLibrariesFolder = destinationLibrariesFolderPath.toFile();
-        if ( !destinationLibrariesFolder.exists() )
-        {
-            destinationLibrariesFolder.mkdir();
-        }
-
-        // Verifying and copying libraries (if needed)
-        for ( String library : libraries )
-        {
-            File destinationLibraryFile = destinationLibrariesFolderPath.append( library
).toFile();
-            if ( !destinationLibraryFile.exists() )
-            {
-                try
-                {
-                    copyResource( sourceLibrariesPath.append( library ), destinationLibraryFile
);
-                }
-                catch ( IOException e )
-                {
-                    CommonUiUtils.reportError( "An error occurred when copying the library
'" + library
-                        + "' to the location '" + destinationLibraryFile.getAbsolutePath()
+ "'.\n\n" + e.getMessage() );
-                }
-            }
-        }
-    }
-
-
-    /**
      * Gets the path to the server libraries folder.
      *
      * @return
@@ -387,120 +245,4 @@ public class ApacheDS156LdapServerAdapte
             return 0;
         }
     }
-
-
-    /**
-     * Starting the "terminate" listener thread.
-     * 
-     * @param server 
-     *      the server
-     * @param launch 
-     *      the launch
-     */
-    private void startTerminateListenerThread( final LdapServer server, final ILaunch launch
)
-    {
-        // Creating the thread
-        Thread thread = new Thread()
-        {
-            /** The debug event listener */
-            private IDebugEventSetListener debugEventSetListener;
-
-
-            public void run()
-            {
-                // Creating the listener
-                debugEventSetListener = new IDebugEventSetListener()
-                {
-                    public void handleDebugEvents( DebugEvent[] events )
-                    {
-                        // Looping on the debug events array
-                        for ( DebugEvent debugEvent : events )
-                        {
-                            // We only care of event with kind equals to
-                            // 'terminate'
-                            if ( debugEvent.getKind() == DebugEvent.TERMINATE )
-                            {
-                                // Getting the source of the debug event
-                                Object source = debugEvent.getSource();
-                                if ( source instanceof RuntimeProcess )
-                                {
-                                    RuntimeProcess runtimeProcess = ( RuntimeProcess ) source;
-
-                                    // Getting the associated launch
-                                    ILaunch debugEventLaunch = runtimeProcess.getLaunch();
-                                    if ( debugEventLaunch.equals( launch ) )
-                                    {
-                                        // The launch we had created is now terminated
-                                        // The server is now stopped
-                                        server.setStatus( LdapServerStatus.STOPPED );
-
-                                        // Removing the listener
-                                        DebugPlugin.getDefault().removeDebugEventListener(
debugEventSetListener );
-
-                                        // ... and we exit the thread
-                                        return;
-                                    }
-                                }
-                            }
-                        }
-                    }
-                };
-
-                // Adding the listener
-                DebugPlugin.getDefault().addDebugEventListener( debugEventSetListener );
-            }
-        };
-
-        // Starting the thread
-        thread.start();
-    }
-
-
-    /**
-    * Copy the given resource.
-    *
-    * @param resource
-    *      the path of the resource
-    * @param destination
-    *      the destination
-    * @throws IOException
-    *      if an error occurs when copying the jar file
-    */
-    private static void copyResource( IPath resource, File destination ) throws IOException
-    {
-        // Getting he URL of the resource within the bundle
-        URL resourceUrl = FileLocator.find( ApacheDS156Plugin.getDefault().getBundle(), resource,
null );
-
-        // Creating the input and output streams
-        InputStream resourceInputStream = resourceUrl.openStream();
-        FileOutputStream resourceOutputStream = new FileOutputStream( destination );
-
-        // Copying the resource
-        copyFile( resourceInputStream, resourceOutputStream );
-
-        // Closing the streams
-        resourceInputStream.close();
-        resourceOutputStream.close();
-    }
-
-
-    /**
-     * Copies a file from the given streams.
-     *
-     * @param inputStream
-     *      the input stream
-     * @param outputStream
-     *      the output stream
-     * @throws IOException
-     *      if an error occurs when copying the file
-     */
-    private static void copyFile( InputStream inputStream, OutputStream outputStream ) throws
IOException
-    {
-        byte[] buf = new byte[1024];
-        int i = 0;
-        while ( ( i = inputStream.read( buf ) ) != -1 )
-        {
-            outputStream.write( buf, 0, i );
-        }
-    }
 }

Modified: directory/studio/trunk/ldapservers/pom.xml
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapservers/pom.xml?rev=981907&r1=981906&r2=981907&view=diff
==============================================================================
--- directory/studio/trunk/ldapservers/pom.xml (original)
+++ directory/studio/trunk/ldapservers/pom.xml Tue Aug  3 14:59:43 2010
@@ -115,6 +115,9 @@
             <Eclipse-LazyStart>true</Eclipse-LazyStart>
             <Bundle-Activator>org.apache.directory.studio.ldapservers.LdapServersPlugin</Bundle-Activator>
             <Require-Bundle>org.eclipse.core.runtime,
+ org.eclipse.debug.core,
+ org.eclipse.debug.ui,
+ org.eclipse.jdt.launching,
  org.eclipse.jface.text,
  org.eclipse.ui,
  org.eclipse.ui.ide,
@@ -213,6 +216,21 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>org.eclipse.debug</groupId>
+      <artifactId>core</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.debug</groupId>
+      <artifactId>ui</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.jdt</groupId>
+      <artifactId>launching</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>org.eclipse.equinox</groupId>
       <artifactId>registry</artifactId>
       <scope>provided</scope>

Modified: directory/studio/trunk/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java?rev=981907&r1=981906&r2=981907&view=diff
==============================================================================
--- directory/studio/trunk/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
(original)
+++ directory/studio/trunk/ldapservers/src/main/java/org/apache/directory/studio/ldapservers/LdapServersUtils.java
Tue Aug  3 14:59:43 2010
@@ -21,9 +21,38 @@
 package org.apache.directory.studio.ldapservers;
 
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.studio.common.ui.CommonUiUtils;
 import org.apache.directory.studio.ldapservers.model.LdapServer;
 import org.apache.directory.studio.ldapservers.model.LdapServerStatus;
 import org.apache.mina.util.AvailablePortFinder;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.debug.core.DebugEvent;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.IDebugEventSetListener;
+import org.eclipse.debug.core.ILaunch;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.debug.core.model.RuntimeProcess;
+import org.eclipse.debug.ui.IDebugUIConstants;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
+import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
+import org.eclipse.jdt.launching.IVMInstall;
+import org.eclipse.jdt.launching.JavaRuntime;
+import org.eclipse.osgi.util.NLS;
+import org.osgi.framework.Bundle;
 
 
 /**
@@ -31,6 +60,10 @@ import org.apache.mina.util.AvailablePor
  */
 public class LdapServersUtils
 {
+    /** The ID of the launch configuration custom object */
+    public static final String LAUNCH_CONFIGURATION_CUSTOM_OBJECT = "launchConfiguration";
+
+
     /**
      * Runs the startup listener watchdog.
      *
@@ -100,4 +133,256 @@ public class LdapServersUtils
             server.setStatus( LdapServerStatus.STOPPED );
         }
     }
+
+
+    /**
+     * Starting the "terminate" listener thread.
+     * 
+     * @param server 
+     *      the server
+     * @param launch 
+     *      the launch
+     */
+    public static void startTerminateListenerThread( final LdapServer server, final ILaunch
launch )
+    {
+        // Creating the thread
+        Thread thread = new Thread()
+        {
+            /** The debug event listener */
+            private IDebugEventSetListener debugEventSetListener;
+
+
+            public void run()
+            {
+                // Creating the listener
+                debugEventSetListener = new IDebugEventSetListener()
+                {
+                    public void handleDebugEvents( DebugEvent[] events )
+                    {
+                        // Looping on the debug events array
+                        for ( DebugEvent debugEvent : events )
+                        {
+                            // We only care of event with kind equals to
+                            // 'terminate'
+                            if ( debugEvent.getKind() == DebugEvent.TERMINATE )
+                            {
+                                // Getting the source of the debug event
+                                Object source = debugEvent.getSource();
+                                if ( source instanceof RuntimeProcess )
+                                {
+                                    RuntimeProcess runtimeProcess = ( RuntimeProcess ) source;
+
+                                    // Getting the associated launch
+                                    ILaunch debugEventLaunch = runtimeProcess.getLaunch();
+                                    if ( debugEventLaunch.equals( launch ) )
+                                    {
+                                        // The launch we had created is now terminated
+                                        // The server is now stopped
+                                        server.setStatus( LdapServerStatus.STOPPED );
+
+                                        // Removing the listener
+                                        DebugPlugin.getDefault().removeDebugEventListener(
debugEventSetListener );
+
+                                        // ... and we exit the thread
+                                        return;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                };
+
+                // Adding the listener
+                DebugPlugin.getDefault().addDebugEventListener( debugEventSetListener );
+            }
+        };
+
+        // Starting the thread
+        thread.start();
+    }
+
+
+    /**
+     * Launches Apache DS using a launch configuration.
+     *
+     * @param server
+     *      the server
+     * @param apacheDsLibrariesFolder
+     *      the ApacheDS libraries folder
+     * @param libraries
+     *      the names of the libraries
+     * @return
+     *      the associated launch
+     */
+    public static ILaunch launchApacheDS( LdapServer server, IPath apacheDsLibrariesFolder,
String[] libraries )
+        throws Exception
+    {
+        // Getting the default VM installation
+        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
+
+        // Creating a new editable launch configuration
+        ILaunchConfigurationType type = DebugPlugin.getDefault().getLaunchManager()
+            .getLaunchConfigurationType( IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION
);
+        ILaunchConfigurationWorkingCopy workingCopy = type.newInstance( null, NLS.bind( "Starting
{0}", new String[]
+            { server.getName() } ) );
+
+        // Setting the JRE container path attribute
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH,
vmInstall
+            .getInstallLocation().toString() );
+
+        // Setting the main type attribute
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
+            "org.apache.directory.studio.apacheds.Launcher" ); //$NON-NLS-1$
+
+        // Creating the classpath list
+        List<String> classpath = new ArrayList<String>();
+        for ( String library : libraries )
+        {
+            IRuntimeClasspathEntry libraryClasspathEntry = JavaRuntime
+                .newArchiveRuntimeClasspathEntry( apacheDsLibrariesFolder.append( library
) );
+            libraryClasspathEntry.setClasspathProperty( IRuntimeClasspathEntry.USER_CLASSES
);
+
+            classpath.add( libraryClasspathEntry.getMemento() );
+        }
+
+        // Setting the classpath type attribute
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath
);
+
+        // Setting the default classpath type attribute to false
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,
false );
+
+        // The server folder path
+        IPath serverFolderPath = LdapServersManager.getServerFolder( server );
+
+        // Setting the program arguments attribute
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
"\"" //$NON-NLS-1$
+            + serverFolderPath.toOSString() + "\"" ); //$NON-NLS-1$
+
+        // Creating the VM arguments string
+        StringBuffer vmArguments = new StringBuffer();
+        vmArguments.append( "-Dlog4j.configuration=file:\"" //$NON-NLS-1$
+            + serverFolderPath.append( "conf" ).append( "log4j.properties" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+        vmArguments.append( " " ); //$NON-NLS-1$
+        vmArguments.append( "-Dapacheds.var.dir=\"" + serverFolderPath.toOSString() + "\""
); //$NON-NLS-1$ //$NON-NLS-2$
+        vmArguments.append( " " ); //$NON-NLS-1$
+        vmArguments.append( "-Dapacheds.log.dir=\"" + serverFolderPath.append( "log" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+        vmArguments.append( " " ); //$NON-NLS-1$
+        vmArguments.append( "-Dapacheds.run.dir=\"" + serverFolderPath.append( "run" ).toOSString()
+ "\"" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+        vmArguments.append( " " ); //$NON-NLS-1$
+        vmArguments.append( "-Dapacheds.instance=\"" + server.getName() + "\"" ); //$NON-NLS-1$
//$NON-NLS-2$
+
+        // Setting the VM arguments attribute
+        workingCopy.setAttribute( IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArguments.toString()
);
+
+        // Setting the launch configuration as private
+        workingCopy.setAttribute( IDebugUIConstants.ATTR_PRIVATE, true );
+
+        // Indicating that we don't want any console to show up
+        workingCopy.setAttribute( DebugPlugin.ATTR_CAPTURE_OUTPUT, false );
+
+        // Saving the launch configuration
+        ILaunchConfiguration configuration = workingCopy.doSave();
+
+        // Launching the launch configuration
+        ILaunch launch = configuration.launch( ILaunchManager.RUN_MODE, new NullProgressMonitor()
);
+
+        // Storing the launch configuration as a custom object in the LDAP Server for later
use
+        server.putCustomObject( LAUNCH_CONFIGURATION_CUSTOM_OBJECT, launch );
+
+        return launch;
+    }
+
+
+    /**
+     * Verifies that the libraries folder exists and contains the jar files 
+     * needed to launch the server.
+     *
+     * @param bundle
+     *      the bundle
+     * @param sourceLibrariesPath
+     *      the path to the source libraries
+     * @param destinationLibrariesPath
+     *      the path to the destination libraries
+     * @param libraries
+     *      the names of the libraries
+     */
+    public static void verifyAndCopyLibraries( Bundle bundle, IPath sourceLibrariesPath,
+        IPath destinationLibrariesPath, String[] libraries )
+    {
+        // Destination libraries folder
+        File destinationLibraries = destinationLibrariesPath.toFile();
+        if ( !destinationLibraries.exists() )
+        {
+            destinationLibraries.mkdir();
+        }
+
+        // Verifying and copying libraries (if needed)
+        for ( String library : libraries )
+        {
+            File destinationLibraryFile = destinationLibrariesPath.append( library ).toFile();
+            if ( !destinationLibraryFile.exists() )
+            {
+                try
+                {
+                    copyResource( bundle, sourceLibrariesPath.append( library ), destinationLibraryFile
);
+                }
+                catch ( IOException e )
+                {
+                    CommonUiUtils.reportError( "An error occurred when copying the library
'" + library
+                        + "' to the location '" + destinationLibraryFile.getAbsolutePath()
+ "'.\n\n" + e.getMessage() );
+                }
+            }
+        }
+    }
+
+
+    /**
+    * Copy the given resource.
+    *
+    * @param bundle
+    *       the bundle
+    * @param resource
+    *      the path of the resource
+    * @param destination
+    *      the destination
+    * @throws IOException
+    *      if an error occurs when copying the jar file
+    */
+    public static void copyResource( Bundle bundle, IPath resource, File destination ) throws
IOException
+    {
+        // Getting he URL of the resource within the bundle
+        URL resourceUrl = FileLocator.find( bundle, resource, null );
+
+        // Creating the input and output streams
+        InputStream resourceInputStream = resourceUrl.openStream();
+        FileOutputStream resourceOutputStream = new FileOutputStream( destination );
+
+        // Copying the resource
+        copyFile( resourceInputStream, resourceOutputStream );
+
+        // Closing the streams
+        resourceInputStream.close();
+        resourceOutputStream.close();
+    }
+
+
+    /**
+     * Copies a file from the given streams.
+     *
+     * @param inputStream
+     *      the input stream
+     * @param outputStream
+     *      the output stream
+     * @throws IOException
+     *      if an error occurs when copying the file
+     */
+    private static void copyFile( InputStream inputStream, OutputStream outputStream ) throws
IOException
+    {
+        byte[] buf = new byte[1024];
+        int i = 0;
+        while ( ( i = inputStream.read( buf ) ) != -1 )
+        {
+            outputStream.write( buf, 0, i );
+        }
+    }
 }



Mime
View raw message