Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.st.core.jaxb;
   18   
   19   import java.io.InputStream;
   20   import java.io.OutputStream;
   21   import java.lang.reflect.Method;
   22   import java.util.ArrayList;
   23   import java.util.Collection;
   24   import java.util.HashMap;
   25   import java.util.Iterator;
   26   import java.util.List;
   27   import java.util.Map;
   28   
   29   import javax.xml.bind.JAXBContext;
   30   import javax.xml.bind.JAXBElement;
   31   
   32   import org.apache.geronimo.st.core.Activator;
   33   import org.apache.geronimo.st.core.internal.Trace;
   34   import org.eclipse.core.resources.IFile;
   35   import org.eclipse.core.resources.IProject;
   36   import org.eclipse.core.runtime.CoreException;
   37   import org.eclipse.core.runtime.IConfigurationElement;
   38   import org.eclipse.core.runtime.IExtensionRegistry;
   39   import org.eclipse.core.runtime.Platform;
   40   import org.eclipse.jst.server.core.FacetUtil;
   41   import org.eclipse.wst.common.project.facet.core.IFacetedProject;
   42   import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
   43   import org.eclipse.wst.server.core.IRuntime;
   44   
   45   /**
   46    * @version $Rev: 824070 $ $Date: 2009-10-11 21:27:37 +0800 (Sun, 11 Oct 2009) $
   47    */
   48   public class JAXBUtils {
   49   
   50       private static Map<String,IJAXBUtilsProvider> providers = new HashMap<String,IJAXBUtilsProvider>();
   51       
   52   	static {
   53   		loadExtensionPoints();
   54   	}
   55   	
   56   	private static synchronized void loadExtensionPoints() {
   57           Trace.tracePoint("ENTRY", "JAXBUtils.loadExtensionPoints");
   58   
   59   		IExtensionRegistry registry = Platform.getExtensionRegistry();
   60   		IConfigurationElement[] cf = registry.getConfigurationElementsFor(Activator.PLUGIN_ID, "JAXBUtilsProvider");
   61   		for (int i = 0; i < cf.length; i++) {
   62   			IConfigurationElement element = cf[i];
   63   			if ("provider".equals(element.getName())) {
   64   				try {
   65   					IJAXBUtilsProvider provider = (IJAXBUtilsProvider) element.createExecutableExtension("class");
   66   					String versions = element.getAttribute("version");
   67   					String[] versionArray = versions.split(",");
   68   					for (int j=0;j<versionArray.length;j++) {
   69   						providers.put(versionArray[j], provider);
   70   					}
   71   				} catch (CoreException e) {
   72                       Trace.tracePoint("CoreException", "JAXBUtils.loadExtensionPoints");
   73   					e.printStackTrace();
   74   				}
   75   			}
   76   		}
   77   
   78           Trace.tracePoint("EXIT", "JAXBUtils.loadExtensionPoints");
   79   	}
   80       
   81       public static List<JAXBContext> getJAXBContext(){
   82       	List<JAXBContext> contextList = new ArrayList<JAXBContext>();
   83       	
   84       	Collection<IJAXBUtilsProvider> jaxbutils =  providers.values();
   85       	Iterator<IJAXBUtilsProvider> iterator = jaxbutils.iterator();
   86       	while (iterator.hasNext()){
   87       		IJAXBUtilsProvider provider = iterator.next();
   88       		contextList.add(provider.getJAXBContext());
   89       	}
   90       	return contextList;
   91       }
   92       
   93       private static IJAXBUtilsProvider getProvider(IFile plan) {
   94           Trace.tracePoint("ENTRY", "JAXBUtils.getProvider");
   95   
   96           IJAXBUtilsProvider provider = null;
   97           if (plan != null) {
   98   				IProject project = plan.getProject();
   99   				try {
  100   					IFacetedProject fp = ProjectFacetsManager.create(project);
  101   					if (fp == null) return null;
  102   					IRuntime runtime = FacetUtil.getRuntime(fp.getPrimaryRuntime());
  103   					if (runtime == null) return null;
  104   					String version = runtime.getRuntimeType().getVersion();
  105   					provider = (IJAXBUtilsProvider) providers.get(version);
  106   				} catch (CoreException e) {
  107                       Trace.tracePoint("CoreException", "JAXBUtils.getProvider");
  108   					e.printStackTrace();
  109   				} catch (IllegalArgumentException ie) {
  110                       Trace.tracePoint("IllegalArgumentException", "JAXBUtils.getProvider");
  111   				    throw new IllegalArgumentException("The project [" + project.getName() + "] does not have a Targeted Runtime specified.");
  112                   }
  113           }
  114           
  115           Trace.tracePoint("EXIT", "JAXBUtils.getProvider", provider);
  116   		return provider;
  117   	}
  118   
  119       public static void marshalDeploymentPlan(JAXBElement jaxbElement, IFile file) throws Exception {
  120           	IJAXBUtilsProvider provider = getProvider(file);
  121           	provider.marshalDeploymentPlan(jaxbElement, file);
  122       }
  123   
  124       public static JAXBElement unmarshalFilterDeploymentPlan(IFile file) throws Exception {
  125       	IJAXBUtilsProvider provider = getProvider(file);
  126       	return provider.unmarshalFilterDeploymentPlan(file);
  127       }
  128   
  129       public static void marshalPlugin(JAXBElement jaxbElement, OutputStream outputStream) throws Exception {
  130           //currently only JAXB21Utils provide this method,so invoke it directly
  131       	providers.get("2.1").marshalPlugin(jaxbElement, outputStream);
  132       }
  133   
  134       public static JAXBElement unmarshalPlugin(InputStream inputStream) {
  135       	//currently only JAXB21Utils provide this method,so invoke it directly
  136       	return providers.get("2.1").unmarshalPlugin(inputStream);
  137       }
  138   
  139       public static Object getValue( Object element, String name ) throws Exception {
  140           try {
  141               if (String.class.isInstance(element))
  142                   return (String)element;
  143               Method method = element.getClass().getMethod( "get" + name, null);
  144               return method.invoke(element, null);
  145           } catch ( Exception e ) {
  146               throw e;
  147           }
  148       }
  149       
  150       public static void setValue( Object element, String name, Object value ) throws Exception {
  151           try {
  152               Method[] methods = element.getClass().getMethods();
  153               for ( Method method: methods) {
  154                   if ( method.getName().equals( "set" + name ) ) {
  155                       method.invoke( element, value );
  156                       return;
  157                   }
  158               }
  159           } catch (Exception e) {
  160               throw e;
  161           }
  162           System.out.println( "============== No such method set" + name + " in class " + element.getClass().getName() );
  163       }
  164   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.core.jaxb » [javadoc | source]