geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r1344906 - in /geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal: GeronimoRuntimeWizardFragment.java Messages.java Messages.properties TargetPlatformHelper.java
Date Thu, 31 May 2012 20:56:38 GMT
Author: gawor
Date: Thu May 31 20:56:37 2012
New Revision: 1344906

URL: http://svn.apache.org/viewvc?rev=1344906&view=rev
Log:
GERONIMODEVTOOLS-794: Use reflection API to set target platform and make it work with older
(3.6, 3.7) and newer (3.8, 4.2) Eclipse versions

Added:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
  (with props)
Modified:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/GeronimoRuntimeWizardFragment.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.properties

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/GeronimoRuntimeWizardFragment.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/GeronimoRuntimeWizardFragment.java?rev=1344906&r1=1344905&r2=1344906&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/GeronimoRuntimeWizardFragment.java
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/GeronimoRuntimeWizardFragment.java
Thu May 31 20:56:37 2012
@@ -17,34 +17,20 @@
 package org.apache.geronimo.st.v30.ui.internal;
 
 import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.apache.geronimo.st.v30.core.GeronimoRuntimeDelegate;
 import org.apache.geronimo.st.v30.ui.Activator;
-import org.apache.geronimo.st.v30.ui.CommonMessages;
-import org.eclipse.core.resources.IWorkspaceRoot;
-import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.core.variables.IStringVariableManager;
-import org.eclipse.core.variables.IValueVariable;
-import org.eclipse.core.variables.VariablesPlugin;
 import org.eclipse.jdt.launching.IVMInstall;
 import org.eclipse.jdt.launching.IVMInstall2;
 import org.eclipse.jdt.launching.IVMInstallType;
@@ -54,11 +40,6 @@ import org.eclipse.jface.preference.IPre
 import org.eclipse.jface.preference.PreferenceDialog;
 import org.eclipse.jface.preference.PreferenceManager;
 import org.eclipse.jface.window.Window;
-import org.eclipse.pde.internal.core.PDECore;
-import org.eclipse.pde.internal.core.target.provisional.ITargetDefinition;
-import org.eclipse.pde.internal.core.target.provisional.ITargetHandle;
-import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService;
-import org.eclipse.pde.internal.core.target.provisional.LoadTargetDefinitionJob;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.BusyIndicator;
 import org.eclipse.swt.events.ModifyEvent;
@@ -91,32 +72,12 @@ import org.eclipse.wst.server.core.model
 import org.eclipse.wst.server.ui.internal.SWTUtil;
 import org.eclipse.wst.server.ui.wizard.IWizardHandle;
 import org.eclipse.wst.server.ui.wizard.WizardFragment;
-import org.osgi.framework.Bundle;
 
 /**
  * @version $Rev$ $Date$
  */
 public class GeronimoRuntimeWizardFragment extends WizardFragment {
 
-    // serverName-N.N or serverName-N.N.N
-    public static final Pattern SERVER_NAME_VERSION_PATTERN = Pattern.compile("(.*-)((\\d+\\.\\d+)(\\.(\\d+))?)");
-    
-    private static final String  LOCAL_TARGET_DIRECTORY = ".metadata/.plugins/org.eclipse.pde.core/.local_targets/";
-    
-    private static final String TARGET_FILE_EXTENSION = ".target";
-    
-    private static final String TARGET_FILE_LOCATIONS_PLACEHOLDER = "@locations_placeholder@";
-    
-    private static final String[] bundleDirectories = { "repository/org/apache/geronimo/specs",
-        "repository/org/apache/geronimo/javamail/geronimo-javamail_1.4_mail",
-        "repository/org/apache/geronimo/bundles/jaxb-impl", "repository/org/apache/geronimo/bundles/jstl",
-        "repository/org/apache/geronimo/bundles/myfaces-bundle",
-        "repository/org/apache/geronimo/framework/geronimo-jdbc",
-        "repository/org/eclipse/osgi",
-        "repository/org/osgi/org.osgi.compendium"};
-    
-    protected final String LOCATION_LINE_TEMPLATE = "<location path=\"${server_location}GERONIMO_BUNDLE_FOLDER_NAME\"
type=\"Directory\"/>";
-
     private GeronimoRuntimeDelegate geronimoRuntime;
 
     protected Text installDir;
@@ -133,26 +94,7 @@ public class GeronimoRuntimeWizardFragme
         super();
     }
  
-    protected String getLocationLineTempalte() {
-    	return LOCATION_LINE_TEMPLATE.replaceAll("server_location", getServerLocationVariableName());
-    }
-    
-    protected String getTargetTemplateFileName() {
-    	return "targets/Geronimo30.target";
-    }
-    
-    protected String getServerLocationVariableName() {
-    	return "geronimo30_server_location";
-    }
-    
-    protected String getServerLocationVariableDescription(){
-    	return Messages.serverLocationVariableDescription;
-    }
-    
-    protected URL getTargetTempateFile() {
-        Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
-        return bundle.getEntry(getTargetTemplateFileName());
-    }
+
     /*
      * (non-Javadoc)
      * 
@@ -453,134 +395,35 @@ public class GeronimoRuntimeWizardFragme
     
     public void performFinish(IProgressMonitor monitor) throws CoreException {
 
-        ITargetPlatformService service = 
-            (ITargetPlatformService) PDECore.getDefault().acquireService(ITargetPlatformService.class.getName());
+        TargetPlatformHelper helper = null;
         
-        ITargetHandle geronimoTargetHandle = findGeronimoTargetDefinition(service);
+        try {
+            helper = TargetPlatformHelper.getTargetPlatformHelper();
+        } catch (Exception e) {
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.targetPlatformError,
e));
+        }
+        
+        Object geronimoTargetHandle = helper.findTargetHandle(getServerVersion());
         
         if (geronimoTargetHandle == null) {
             // target definition not found - generate one 
-            
             String geronimoServerLocation = getServerLocation();
             
-            // add or update string substitution variable named ${geronimo_30_server_location}

-            IStringVariableManager varManager = VariablesPlugin.getDefault().getStringVariableManager();
-            IValueVariable v = varManager.getValueVariable(getServerLocationVariableName());
-            if (v == null) {
-                v = varManager.newValueVariable(getServerLocationVariableName(),
-                								getServerLocationVariableDescription(), 
-                                                false, 
-                                                geronimoServerLocation);
-                varManager.addVariables(new IValueVariable[] { v });
-            } else {
-                v.setValue(geronimoServerLocation);
-            }
-                        
-            // copy the target file for target platform into workspace
-            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
-            IPath rootLocation = root.getLocation();
-            String timestamp = Long.toString(System.currentTimeMillis());           
-            String append = LOCAL_TARGET_DIRECTORY + timestamp + TARGET_FILE_EXTENSION;
-            IPath destination = rootLocation.append(append);            
-            
-            URL template = getTargetTempateFile();
+            helper.createTargetPlatform(geronimoServerLocation);
             
-            copyFile(template, destination, getLocationEntryLists(geronimoServerLocation));
-                        
-            geronimoTargetHandle = findGeronimoTargetDefinition(service);               
    
+            geronimoTargetHandle = helper.findTargetHandle(getServerVersion());
         }
         
-        ITargetHandle activeTargetHandle = service.getWorkspaceTargetHandle();
-        
         if (geronimoTargetHandle != null) {
-            if (activeTargetHandle == null || !activeTargetHandle.equals(geronimoTargetHandle))
{
-                ITargetDefinition geronimoTarget = geronimoTargetHandle.getTargetDefinition();
-                LoadTargetDefinitionJob.load(geronimoTarget);
-            }            
+            helper.switchTargetPlatform(geronimoTargetHandle);        
         }
+        
     }
     
     protected String getServerVersion() {
     	return Messages.serverVersion;
     }
     
-    private ITargetHandle findGeronimoTargetDefinition(ITargetPlatformService service) throws
CoreException {
-        ITargetHandle[] targetHandles = service.getTargets(null);
-        for (ITargetHandle cur : targetHandles) {
-            if (cur.getTargetDefinition().getName().equals(getServerVersion())) {
-                return cur;
-            }
-        }
-        return null;
-    }
-    
-    protected void copyFile(URL source, IPath dest, String replacement) throws CoreException
{
-        Reader reader = null;
-        Writer writer = null;
-        
-        try {
-            try {
-                reader = new InputStreamReader(source.openStream());
-                
-                StringBuilder builder = new StringBuilder();
-                char [] buf = new char [2048];
-                int count = 0;
-                
-                while ( (count = reader.read(buf)) >= 0) {
-                    builder.append(buf, 0, count);
-                }
-                
-                String template = builder.toString();
-                String text = template.replace(TARGET_FILE_LOCATIONS_PLACEHOLDER, replacement);
-                
-                dest.toFile().getParentFile().mkdirs();
-                writer = new FileWriter(dest.toFile());
-                
-                writer.write(text);
-                
-            } finally {
-                if (reader != null) {
-                    reader.close();
-                }
-                if (writer != null) {
-                    writer.close();
-                }
-            }
-        } catch (IOException ex) {
-            throw new CoreException(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.copyTargetFileFailed,
ex));
-        }
-    }
-    
-    
-    protected String getLocationEntryLists(String serverLocation) {
-
-        StringBuffer buf = new StringBuffer(1024);
-        buf.append(System.getProperty("line.separator"));
-        for (String bundleDir : bundleDirectories) {
-            File directory = new File(serverLocation, bundleDir);
-            listFile(directory, buf, serverLocation);
-        }
-        return buf.toString();
-    }
-
-    private void listFile(File file, StringBuffer buf, String serverLocation) {
-        if (file.isDirectory()) {
-            File[] files = file.listFiles();
-            for (File subfile : files) {
-                listFile(subfile, buf, serverLocation);
-            }
-        } else {
-            String filename = file.getAbsolutePath();
-            if (filename.endsWith(".jar")) {
-                String parentPath = file.getParent();
-                String bundleFolder = parentPath.substring(serverLocation.length());
-                buf.append(getLocationLineTempalte().replaceAll("GERONIMO_BUNDLE_FOLDER_NAME",
-                        Matcher.quoteReplacement(bundleFolder)));
-                buf.append(System.getProperty("line.separator"));
-            }
-        }
-    }
-    
     private GeronimoRuntimeDelegate getRuntimeDelegate() {
         IRuntimeWorkingCopy wc = (IRuntimeWorkingCopy) getTaskModel().getObject(TaskModel.TASK_RUNTIME);
         if (wc == null)
@@ -655,26 +498,4 @@ public class GeronimoRuntimeWizardFragme
         list.add(new GeronimoRuntimeSourceWizardFragment());
     }
 
-    /**
-     * Code for testing server name determination code in the
-     * updateInstallDir(IPath) method of addInstallableRuntimeSection.
-     * 
-     * @param args
-     */
-    public static void main(String[] args) {
-        Pattern SERVER_NAME_VERSION_PATTERN = Pattern.compile("(.*-)((\\d+\\.\\d+)(\\.(\\d+))?)");
-        for (String path : args) {
-            StringBuffer installPath = new StringBuffer();
-            Matcher matcher = SERVER_NAME_VERSION_PATTERN.matcher(path);
-            if (matcher.find()) {
-                String serverName = matcher.group(1);
-                String serverVersion = matcher.group(2);
-                installPath = installPath.append(serverName + serverVersion);
-                System.out.println("path = " + path + ", serverVersion = " + serverVersion
+ ", installPath = "
-                        + installPath);
-            } else {
-                System.out.println("No version found in path = " + path);
-            }
-        }
-    }
 }
\ No newline at end of file

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.java?rev=1344906&r1=1344905&r2=1344906&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.java
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.java
Thu May 31 20:56:37 2012
@@ -617,6 +617,7 @@ public class Messages extends NLS {
 	public static String tooltip;
 	
 	public static String serverVersion;
+	public static String targetPlatformError;
 	public static String copyTargetFileFailed;
 	public static String supportWebPage0;
 	public static String supportURL;

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.properties
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.properties?rev=1344906&r1=1344905&r2=1344906&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.properties
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/Messages.properties
Thu May 31 20:56:37 2012
@@ -454,6 +454,8 @@ tooltipInstall=Download the selected {0}
 tooltipJetty=Selects the Jetty distribution of Geronimo to install.
 tooltipTomcat=Selects the Tomcat distribution of Geronimo to install.
 tooltip=Tool tip
+
+targetPlatformError=Error setting up target platform.
 copyTargetFileFailed=Copy target file failed.
 
 hostName=Hostname:

Added: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java?rev=1344906&view=auto
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
(added)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
Thu May 31 20:56:37 2012
@@ -0,0 +1,310 @@
+/*
+ * 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.geronimo.st.v30.ui.internal;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.regex.Matcher;
+
+import org.apache.geronimo.st.v30.ui.Activator;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.variables.IStringVariableManager;
+import org.eclipse.core.variables.IValueVariable;
+import org.eclipse.core.variables.VariablesPlugin;
+import org.eclipse.pde.internal.core.PDECore;
+import org.osgi.framework.Bundle;
+
+/**
+ * The TargetPlatformHelper uses Eclipse Target Platform API to query and set the default
target platform in Eclipse.
+ * 
+ * TargetPlatformHelper uses reflection API to interact with the Eclipse Target Platform
API since the Eclipse API have
+ * into different packages starting in Eclipse 3.8. Using reflection allows us to have one
version of the plugin that 
+ * works with multiple Eclipse versions and makes building the code a bit easier.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class TargetPlatformHelper  {
+
+    private static final String LOCAL_TARGET_DIRECTORY = ".metadata/.plugins/org.eclipse.pde.core/.local_targets/";
+    
+    private static final String TARGET_FILE_EXTENSION = ".target";
+    
+    private static final String TARGET_FILE_LOCATIONS_PLACEHOLDER = "@locations_placeholder@";
+    
+    private static final String[] bundleDirectories = { "repository/org/apache/geronimo/specs",
+        "repository/org/apache/geronimo/javamail/geronimo-javamail_1.4_mail",
+        "repository/org/apache/geronimo/bundles/jaxb-impl", "repository/org/apache/geronimo/bundles/jstl",
+        "repository/org/apache/geronimo/bundles/myfaces-bundle",
+        "repository/org/apache/geronimo/framework/geronimo-jdbc",
+        "repository/org/eclipse/osgi",
+        "repository/org/osgi/org.osgi.compendium"};
+    
+    private final String LOCATION_LINE_TEMPLATE = "<location path=\"${server_location}GERONIMO_BUNDLE_FOLDER_NAME\"
type=\"Directory\"/>";
+    
+    private final Method getNameMethod;
+    private final Method getTargetDefinitionMethod;
+    private final Method getWorkspaceTargetHandleMethod;
+    private final Method getTargetsMethod;
+    private final Method loadMethod;
+    
+    private Object targetPlatformService;
+
+    private TargetPlatformHelper(String basePackage) throws Exception {
+        Class<?> targetDefinitionClass = Class.forName(basePackage + ".ITargetDefinition");
+        getNameMethod = targetDefinitionClass.getMethod("getName");
+        
+        Class<?> targetHandleClass = Class.forName(basePackage + ".ITargetHandle");
+        getTargetDefinitionMethod = targetHandleClass.getMethod("getTargetDefinition");
+        
+        Class<?> targetPlatformServiceClass = Class.forName(basePackage + ".ITargetPlatformService");
+        getWorkspaceTargetHandleMethod = targetPlatformServiceClass.getMethod("getWorkspaceTargetHandle");
+        getTargetsMethod = targetPlatformServiceClass.getMethod("getTargets", IProgressMonitor.class);
+        
+        Class<?> loadTargetDefinitionClass =  Class.forName(basePackage + ".LoadTargetDefinitionJob");
+        loadMethod = loadTargetDefinitionClass.getMethod("load", targetDefinitionClass);
+        
+        targetPlatformService = PDECore.getDefault().acquireService(basePackage + ".ITargetPlatformService");
+        
+        if (targetPlatformService == null) {
+            throw new Exception("No TargetPlatformService");
+        }
+    }
+    
+    public static TargetPlatformHelper getTargetPlatformHelper() throws Exception {
+        try {
+            Class.forName("org.eclipse.pde.internal.core.target.provisional.ITargetDefinition");
+            return new TargetPlatformHelper("org.eclipse.pde.internal.core.target.provisional");
+        } catch (ClassNotFoundException e) {
+            try {
+                Class.forName("org.eclipse.pde.core.target.ITargetDefinition");
+                return new TargetPlatformHelper("org.eclipse.pde.core.target");
+            } catch (ClassNotFoundException ee) {
+                throw new RuntimeException("Target Platform API are not available");
+            }
+        }
+    }
+    
+    public Object findTargetHandle(String name) throws CoreException {
+        Object[] targetHandles = getTargetHandles(targetPlatformService);
+        if (targetHandles != null) {
+            for (Object targetHandle : targetHandles) {
+                Object targetDefinition = getTargetDefinition(targetHandle);
+                if (targetDefinition != null) {
+                    String targetName = getTargetDefinitionName(targetDefinition);
+                    if (name.equals(targetName)) {
+                        return targetHandle;
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    public void createTargetPlatform(String geronimoServerLocation) throws CoreException
{
+        // add or update string substitution variable named ${geronimo_30_server_location}

+        IStringVariableManager varManager = VariablesPlugin.getDefault().getStringVariableManager();
+        IValueVariable v = varManager.getValueVariable(getServerLocationVariableName());
+        if (v == null) {
+            v = varManager.newValueVariable(getServerLocationVariableName(),
+                                            getServerLocationVariableDescription(), 
+                                            false, 
+                                            geronimoServerLocation);
+            varManager.addVariables(new IValueVariable[] { v });
+        } else {
+            v.setValue(geronimoServerLocation);
+        }
+                    
+        // copy the target file for target platform into workspace
+        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+        IPath rootLocation = root.getLocation();
+        String timestamp = Long.toString(System.currentTimeMillis());           
+        String append = LOCAL_TARGET_DIRECTORY + timestamp + TARGET_FILE_EXTENSION;
+        IPath destination = rootLocation.append(append);            
+        
+        URL template = getTargetTempateFile();
+        
+        copyFile(template, destination, getLocationEntryLists(geronimoServerLocation));
+    }
+    
+    private void copyFile(URL source, IPath dest, String replacement) throws CoreException
{
+        Reader reader = null;
+        Writer writer = null;
+        
+        try {
+            try {
+                reader = new InputStreamReader(source.openStream());
+                
+                StringBuilder builder = new StringBuilder();
+                char [] buf = new char [2048];
+                int count = 0;
+                
+                while ( (count = reader.read(buf)) >= 0) {
+                    builder.append(buf, 0, count);
+                }
+                
+                String template = builder.toString();
+                String text = template.replace(TARGET_FILE_LOCATIONS_PLACEHOLDER, replacement);
+                
+                dest.toFile().getParentFile().mkdirs();
+                writer = new FileWriter(dest.toFile());
+                
+                writer.write(text);
+                
+            } finally {
+                if (reader != null) {
+                    reader.close();
+                }
+                if (writer != null) {
+                    writer.close();
+                }
+            }
+        } catch (IOException ex) {
+            throw new CoreException(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.copyTargetFileFailed,
ex));
+        }
+    }
+    
+    private String getLocationEntryLists(String serverLocation) {
+        StringBuffer buf = new StringBuffer(1024);
+        buf.append(System.getProperty("line.separator"));
+        for (String bundleDir : bundleDirectories) {
+            File directory = new File(serverLocation, bundleDir);
+            listFile(directory, buf, serverLocation);
+        }
+        return buf.toString();
+    }
+
+    private void listFile(File file, StringBuffer buf, String serverLocation) {
+        if (file.isDirectory()) {
+            File[] files = file.listFiles();
+            for (File subfile : files) {
+                listFile(subfile, buf, serverLocation);
+            }
+        } else {
+            String filename = file.getAbsolutePath();
+            if (filename.endsWith(".jar")) {
+                String parentPath = file.getParent();
+                String bundleFolder = parentPath.substring(serverLocation.length());
+                buf.append(getLocationLineTemplate().replaceAll("GERONIMO_BUNDLE_FOLDER_NAME",

+                                                                Matcher.quoteReplacement(bundleFolder)));
+                buf.append(System.getProperty("line.separator"));
+            }
+        }
+    }
+    
+    private String getLocationLineTemplate() {
+        return LOCATION_LINE_TEMPLATE.replaceAll("server_location", getServerLocationVariableName());
+    }
+    
+    private String getTargetTemplateFileName() {
+        return "targets/Geronimo30.target";
+    }
+    
+    private String getServerLocationVariableName() {
+        return "geronimo30_server_location";
+    }
+    
+    private String getServerLocationVariableDescription(){
+        return Messages.serverLocationVariableDescription;
+    }
+    
+    private URL getTargetTempateFile() {
+        Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
+        return bundle.getEntry(getTargetTemplateFileName());
+    }
+    
+    public void switchTargetPlatform(Object targetHandle) throws CoreException {
+        Object activeTargetHandle = getWorkspaceTargetHandle();
+        if (activeTargetHandle == null || !activeTargetHandle.equals(targetHandle)) {
+            Object targetDefinition = getTargetDefinition(targetHandle);
+            if (targetDefinition != null) {
+                setTargetPlatform(targetDefinition);
+            }
+        }
+    }  
+    
+    // Key Target Platform reflection calls 
+
+    private String getTargetDefinitionName(Object targetDefinition) {
+        try {
+            return (String) getNameMethod.invoke(targetDefinition);           
+        } catch (Exception e) {
+            Trace.trace(Trace.ERROR, "Could not get target definition name", e, Activator.traceInternal);
+            return null;
+        } 
+    }
+    
+    private Object getTargetDefinition(Object targetHandle) throws CoreException {
+        try {
+            return getTargetDefinitionMethod.invoke(targetHandle);    
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof CoreException) {
+                throw (CoreException) cause;
+            }
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, cause.getMessage(),
cause));
+        } catch (Exception e) {
+            Trace.trace(Trace.ERROR, "Could not get target definition", e, Activator.traceInternal);
+            return null;
+        } 
+    }
+            
+    private Object[] getTargetHandles(Object targetPlatformService) {
+        try {
+            return (Object[]) getTargetsMethod.invoke(targetPlatformService, new Object[]
{ null });           
+        } catch (Exception e) {
+            Trace.trace(Trace.ERROR, "Could not get platform targets", e, Activator.traceInternal);
+            return null;
+        } 
+    }
+    
+    private Object getWorkspaceTargetHandle() throws CoreException {
+        try {
+            return getWorkspaceTargetHandleMethod.invoke(targetPlatformService);
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof CoreException) {
+                throw (CoreException) cause;
+            }
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, cause.getMessage(),
cause));        
+        } catch (Exception e) {
+            Trace.trace(Trace.ERROR, "Could not get workspace target handle", e, Activator.traceInternal);
+            return null;
+        } 
+    }
+    
+    private void setTargetPlatform(Object targetDefinition) {
+        try {
+            loadMethod.invoke(null, targetDefinition);
+        } catch (Exception e) {
+            Trace.trace(Trace.ERROR, "Could not set target platform", e, Activator.traceInternal);
+        }        
+    }
+}

Propchange: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.ui/src/main/java/org/apache/geronimo/st/v30/ui/internal/TargetPlatformHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message