Author: jgallimore
Date: Tue Jul 29 14:19:15 2008
New Revision: 680845

URL: http://svn.apache.org/viewvc?rev=680845&view=rev
Log:
OPENEJB-869 Fixed publish - added ServerDeployer class which uses the 
DeployerBusinessRemote bean to deploy and undeploy modules from a running server

Added:
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.apache.openejb.helper.annotation/src/main/java/org/apache/openejb/helper/annotation/actions/ConvertToFacetedProjectAction.java
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/ServerDeployer.java
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/test/
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/test/java/
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/test/resources/
Modified:
    
openejb/trunk/sandbox/openejb-eclipse-plugin/common/src/test/java/org/apache/openejb/plugins/common/JDTFacadeStub.java
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/plugin.xml
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/pom.xml
    
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/OpenEJBServerBehaviour.java

Modified: 
openejb/trunk/sandbox/openejb-eclipse-plugin/common/src/test/java/org/apache/openejb/plugins/common/JDTFacadeStub.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/common/src/test/java/org/apache/openejb/plugins/common/JDTFacadeStub.java?rev=680845&r1=680844&r2=680845&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/common/src/test/java/org/apache/openejb/plugins/common/JDTFacadeStub.java
 (original)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/common/src/test/java/org/apache/openejb/plugins/common/JDTFacadeStub.java
 Tue Jul 29 14:19:15 2008
@@ -8,7 +8,7 @@
 
        public void addAnnotationToFieldsOfType(String targetClass, Class<?> 
annotation, Map<String, Object> properties) {
                StringBuffer buffer = new StringBuffer();
-               buffer.append("Target field type: ");
+               buffer.append("[ADD ANNOTATIONS TO FIELDS OF TYPE] Target field 
type: ");
                buffer.append(targetClass);
                buffer.append("\nAnnotation: ");
                buffer.append(annotation.getCanonicalName());
@@ -40,11 +40,17 @@
                        buffer.append("=");
                        
                        Object value = properties.get(key);
-                       if (value instanceof Map) {
+                       if (value == null) {
+                       
+                       } else if (value instanceof Map) {
                                buffer.append(" {\n");
                                writeMapToBuffer((Map<String, Object>) value, 
buffer, indent + 1);
                                buffer.append(indentStr);
                                buffer.append("}");
+                       } else if (value.getClass().isEnum()) {
+                               
buffer.append(value.getClass().getCanonicalName());
+                               buffer.append(".");
+                               buffer.append(value.toString());
                        } else {
                                buffer.append(value.toString());
                        }
@@ -55,7 +61,7 @@
 
        public void addClassAnnotation(String targetClass, Class<? extends 
Annotation> annotation, Map<String, Object> properties) {
                StringBuffer buffer = new StringBuffer();
-               buffer.append("Target class: ");
+               buffer.append("[ADD CLASS ANNOTATION] Target class: ");
                buffer.append(targetClass);
                buffer.append("\nAnnotation: ");
                buffer.append(annotation.getCanonicalName());
@@ -67,13 +73,22 @@
        }
 
        public void addField(String targetClass, String fieldName, String 
fieldType) {
-               // TODO Auto-generated method stub
-
+               StringBuffer buffer = new StringBuffer();
+               buffer.append("[ADD FIELD] Target class: ");
+               buffer.append(targetClass);
+               buffer.append("\n");
+               buffer.append("Field type: ");
+               buffer.append(fieldType);
+               buffer.append("\n");
+               buffer.append("Field name: ");
+               buffer.append(fieldName);
+               buffer.append("\n");
+               System.out.println(buffer.toString());
        }
 
        public void addFieldAnnotation(String targetClass, String targetField, 
Class<?> annotation, Map<String, Object> properties) {
                StringBuffer buffer = new StringBuffer();
-               buffer.append("Target field type: ");
+               buffer.append("[ADD FIELD ANNOTATION] Target field type: ");
                buffer.append(targetClass);
                buffer.append("\nTarget field: ");
                buffer.append(targetField);
@@ -93,7 +108,7 @@
 
        public void addMethodAnnotation(String fullyQualifiedClassName, String 
methodName, String[] signature, Class<?> annotationClass, Map<String, Object> 
properties) {
                StringBuffer buffer = new StringBuffer();
-               buffer.append("Target field type: ");
+               buffer.append("[ADD METHOD ANNOTATION] Target field type: ");
                buffer.append(fullyQualifiedClassName);
                buffer.append("\nTarget field: ");
                buffer.append(methodName);

Added: 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.apache.openejb.helper.annotation/src/main/java/org/apache/openejb/helper/annotation/actions/ConvertToFacetedProjectAction.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.apache.openejb.helper.annotation/src/main/java/org/apache/openejb/helper/annotation/actions/ConvertToFacetedProjectAction.java?rev=680845&view=auto
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.apache.openejb.helper.annotation/src/main/java/org/apache/openejb/helper/annotation/actions/ConvertToFacetedProjectAction.java
 (added)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.apache.openejb.helper.annotation/src/main/java/org/apache/openejb/helper/annotation/actions/ConvertToFacetedProjectAction.java
 Tue Jul 29 14:19:15 2008
@@ -0,0 +1,69 @@
+package org.apache.openejb.helper.annotation.actions;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.IWorkbenchWindowActionDelegate;
+
+public class ConvertToFacetedProjectAction implements IObjectActionDelegate, 
IWorkbenchWindowActionDelegate{
+
+       private static final String FACET_NATURE = 
"org.eclipse.wst.common.project.facet.core.nature";
+       protected IProject project;
+
+       public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+       }
+
+       public void run(IAction action) {
+               if (project == null) {
+                       return;
+               }
+
+               try {
+                       IProjectDescription projectDescription = 
project.getDescription();
+                       String[] natureIds = projectDescription.getNatureIds();
+                       for (String natureId : natureIds) {
+                               if (FACET_NATURE.equals(natureId)) {
+                                       return;
+                               }
+                       }
+                       
+                       String[] newNatureIds = new String[natureIds.length + 
1];
+                       
+                       System.arraycopy(natureIds, 0, newNatureIds, 0, 
natureIds.length);
+                       newNatureIds[natureIds.length] = FACET_NATURE;
+                       projectDescription.setNatureIds(newNatureIds);
+                       project.setDescription(projectDescription, new 
NullProgressMonitor());
+               } catch (CoreException e) {
+               }
+       }
+
+
+       public void selectionChanged(IAction action, ISelection selection) {
+               if (selection instanceof IStructuredSelection) {
+                       Object firstElement = ((IStructuredSelection) 
selection).getFirstElement();
+                       
+                       if (firstElement instanceof IProject) {
+                               project = (IProject) firstElement;
+                       }
+               }
+               
+       }
+
+       public void dispose() {
+               // TODO Auto-generated method stub
+               
+       }
+
+       public void init(IWorkbenchWindow window) {
+               // TODO Auto-generated method stub
+               
+       }
+
+}

Modified: 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/plugin.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/plugin.xml?rev=680845&r1=680844&r2=680845&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/plugin.xml
 (original)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/plugin.xml
 Tue Jul 29 14:19:15 2008
@@ -28,7 +28,7 @@
            version="3.0.0">
              <moduleType
                 types="jst.ejb"
-                versions="2.1, 3.0"/>
+                versions="1.0, 2.0, 2.1, 3.0"/>
            </runtimeType>
        </extension>
 

Modified: 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/pom.xml?rev=680845&r1=680844&r2=680845&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/pom.xml
 (original)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/pom.xml
 Tue Jul 29 14:19:15 2008
@@ -54,6 +54,13 @@
                                </includes>
                        </resource>
                        <resource>
+                               <directory>lib</directory>
+                               <targetPath>lib</targetPath>
+                               <includes>
+                                       <include>*</include>
+                               </includes>
+                       </resource>
+                       <resource>
                                <directory>icons</directory>
                                <targetPath>icons</targetPath>
                                <includes>

Modified: 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/OpenEJBServerBehaviour.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/OpenEJBServerBehaviour.java?rev=680845&r1=680844&r2=680845&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/OpenEJBServerBehaviour.java
 (original)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/OpenEJBServerBehaviour.java
 Tue Jul 29 14:19:15 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.openejb.eclipse.server;
 
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -24,7 +25,10 @@
 import java.lang.reflect.Field;
 import java.net.Socket;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -35,7 +39,6 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
@@ -89,12 +92,16 @@
                                if (! running) {
                                        running = true;
                                        setState(IServer.STATE_STARTED);
+                                       
+                                       // republish everything
+                                       doFullPublish();
                                }
                        } catch (IOException e) {
                                if (running) {
                                        // looks like server has started 
successfully, but has died
                                        setServerState(IServer.STATE_STOPPED);
                                        running = false;
+                                       cleanup();
                                }
                                // server might not be started yet
                        }
@@ -111,6 +118,7 @@
        }
        
        private ServerMonitor monitor;
+       private Map<IModule, String> publishedModules = new HashMap<IModule, 
String>();
        
        @Override
        public void stop(boolean force) {
@@ -176,68 +184,99 @@
                monitor.start();
        }
 
+       private void doFullPublish() {
+               Iterator<IModule> iterator = 
publishedModules.keySet().iterator();
+               while (iterator.hasNext()) {
+                       IModule module = (IModule) iterator.next();
+                       doPublish(module, ADDED);
+               }
+       }
+       
+       private void cleanup() {
+               Iterator<IModule> iterator = 
publishedModules.keySet().iterator();
+               while (iterator.hasNext()) {
+                       IModule module = (IModule) iterator.next();
+                       String publishedFile = publishedModules.get(module);
+                       if (publishedFile != null) {
+                               new File(publishedFile).delete();
+                       }
+               }
+       }
+       
+       private void doPublish(IModule module, int kind) {
+               ServerDeployer serverDeployer = new 
ServerDeployer(getRuntimeDelegate().getRuntime().getLocation().toFile().getAbsolutePath());
+               
+               // if module already published, try an undeploy first, and 
cleanup temp file
+               String jarFile = publishedModules.get(module);
+               if (jarFile != null) {
+                       serverDeployer.undeploy(jarFile);
+                       new File(jarFile).delete();
+                       publishedModules.remove(module);
+               }
+               
+               if (kind == REMOVED) {
+                       return;
+               }
+               
+               // now do the export
+               String newJarFile = exportModule(module);
+               
+               // publish the new export
+               if (newJarFile != null) {
+                       String path = serverDeployer.deploy(newJarFile);
+                       publishedModules.put(module, path);
+               }
+       }
+       
        @Override
        protected IStatus publishModule(int kind, IModule[] modules, int 
deltaKind, IProgressMonitor monitor) {
-               IPath appsDir = 
getRuntimeDelegate().getRuntime().getLocation().append("/apps");
-               
-               if (deltaKind == REMOVED) {
-                       StringBuffer removedMsg = new StringBuffer();
-                       
+               if (IServer.STATE_STARTED != getServer().getServerState()) {
                        for (IModule module : modules) {
-                               File target = new File(appsDir.toFile(), 
module.getName() + ".jar");
-                               if (target.exists() && target.isFile()) {
-                                       boolean deleted = target.delete();
-                                       
-                                       if (! deleted) {
-                                               removedMsg.append("Unable to 
delete module: " + target.getAbsolutePath() + " from server\n");
+                               if (kind == REMOVED) {
+                                       String jarFile = 
publishedModules.get(module);
+                                       if (jarFile != null) {
+                                               new File(jarFile).delete();
                                        }
+                                       
+                                       publishedModules.remove(module);
+                               } else {
+                                       publishedModules.put(module, null);
                                }
                        }
-                       
-                       if (removedMsg.length() == 0) {
-                               return new Status(IStatus.OK, 
"org.eclipse.jst.server.generic.openejb", "");
-                       } else {
-                               return new Status(IStatus.WARNING, 
"org.eclipse.jst.server.generic.openejb", removedMsg.toString());
+               } else {
+                       for (IModule module : modules) {
+                               doPublish(module, deltaKind);
                        }
                }
                
+               return super.publishModule(kind, modules, deltaKind, monitor);
+       }
+
+       protected String exportModule(IModule module) {
                try {
-                       for (IModule module : modules) {
-                               ProjectModule projectModule = (ProjectModule) 
module.loadAdapter(ProjectModule.class, null);
-                               if (projectModule == null) {
-                                       continue;
-                               }
-                               
-                               IModuleResource[] members = 
projectModule.members();
+                       ProjectModule projectModule = (ProjectModule) 
module.loadAdapter(ProjectModule.class, null);
+                       if (projectModule == null) {
+                               return null;
+                       }
+                       
+                       IModuleResource[] members = projectModule.members();
 
-                               try {
-                                       File tempJarFile = 
File.createTempFile("oejb", ".jar");
-                                       ZipOutputStream zos = new 
ZipOutputStream(new FileOutputStream(tempJarFile));
+                       File tempJarFile = File.createTempFile("oejb", ".jar");
+                       ZipOutputStream zos = new ZipOutputStream(new 
FileOutputStream(tempJarFile));
                        
-                                       
-                                       for (IModuleResource resource : 
members) {
-                                               IPath projectLocation = 
module.getProject().getLocation();
-                                               
-                                               writeResourceToZipStream(zos, 
resource, projectLocation);
-                                                
-                                       }
-                                       
-                                       zos.close();
-                                       
-                                       File target = new 
File(appsDir.toFile(), module.getName() + ".jar");
-                                       
-                                       tempJarFile.renameTo(target);
-                               } catch (IOException e) {
-                                       e.printStackTrace();
-                               }
+                       for (IModuleResource resource : members) {
+                               IPath projectLocation = 
module.getProject().getLocation();
+                               writeResourceToZipStream(zos, resource, 
projectLocation);
                        }
-               } catch (CoreException e) {
-                       e.printStackTrace();
+                       
+                       zos.close();
+                       
+                       return tempJarFile.getAbsolutePath();
+               } catch (Exception e) {
+                       return null;
                }
-
-               return super.publishModule(kind, modules, deltaKind, monitor);
        }
-
+       
        private void writeResourceToZipStream(ZipOutputStream zipStream, 
IModuleResource resource, IPath projectLocation) throws IOException, 
FileNotFoundException {
                byte[] buffer = new byte[8192];
                if (resource instanceof IModuleFile) {

Added: 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/ServerDeployer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/ServerDeployer.java?rev=680845&view=auto
==============================================================================
--- 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/ServerDeployer.java
 (added)
+++ 
openejb/trunk/sandbox/openejb-eclipse-plugin/plugins/org.eclipse.jst.server.generic.openejb/src/main/java/org/apache/openejb/eclipse/server/ServerDeployer.java
 Tue Jul 29 14:19:15 2008
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.openejb.eclipse.server;
+
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+
+
+public class ServerDeployer {
+       private String openejbDir;
+       
+       public ServerDeployer(String openejbDir) {
+               super();
+               this.openejbDir = openejbDir;
+       }
+
+       public String deploy(String filename) {
+               Object obj = callDeployerBusinessRemote("deploy", filename);
+               if (obj == null) {
+                       return null;
+               }
+               
+               try {
+                       return (String) 
obj.getClass().getDeclaredField("jarPath").get(obj);
+               } catch (Exception e) {
+                       return null;
+               }
+       }
+
+       private Object callDeployerBusinessRemote(String methodName, String 
filename) {
+               ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+               try {
+                       Properties properties =  new Properties();
+                       properties.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.openejb.client.RemoteInitialContextFactory");
+                       properties.put(Context.PROVIDER_URL, 
"ejbd://localhost:4201");
+                       
+                       URL[] urls = getUrls(openejbDir);
+                       URLClassLoader cl = new URLClassLoader(urls, 
classLoader);
+                       Thread.currentThread().setContextClassLoader(cl);
+                       
+                       InitialContext context = new InitialContext(properties);
+                       Object ref = 
context.lookup("openejb/DeployerBusinessRemote");
+                       
+                       Method method = ref.getClass().getMethod(methodName, 
String.class);
+                       return method.invoke(ref, filename);
+               } catch (Exception e) {
+                       return null;
+               } finally {
+                       
Thread.currentThread().setContextClassLoader(classLoader);
+               }
+       }
+
+       public boolean undeploy(String filename) {
+               return callDeployerBusinessRemote("undeploy", filename) != null;
+       }
+
+       private URL[] getUrls(String directory) {
+               List<URL> urlList = new ArrayList<URL>();
+               File openEjbDir = new File(directory + File.separator + "lib");
+               File[] files = openEjbDir.listFiles();
+               
+               for (File file : files) {
+                       if (file.getName().endsWith(".jar")) {
+                               try {
+                                       
urlList.add(file.getAbsoluteFile().toURL());
+                               } catch (MalformedURLException e) {
+                               }
+                       }
+               }
+
+               return urlList.toArray(new URL[urlList.size()]);
+       }
+}


Reply via email to