summaryrefslogtreecommitdiffstats
blob: 1eb0ae67867fede67102c143936399738631b862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.blam;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.update.core.IFeature;
import org.eclipse.update.core.ISite;
import org.eclipse.update.core.ISiteFeatureReference;
import org.eclipse.update.core.SiteManager;
import org.eclipse.update.operations.IOperationFactory;
import org.eclipse.update.operations.OperationsManager;

/**
 * @author Ryan D. Brooks
 */
public abstract class DynamicBlamOperation {
   private Method mainMethod;
   private BlamParameter[] parameters;

   public void installAssociatedPlugin() throws MalformedURLException, CoreException {
      IOperationFactory operationFactory = OperationsManager.getOperationFactory();
      ISite site = SiteManager.getSite(new URL("http://www.eclipse.org/osee/"), null);
      ISiteFeatureReference[] refs = site.getFeatureReferences();
      IFeature feature = refs[0].getFeature(null);
      operationFactory.createInstallOperation(site.getCurrentConfiguredSite(), feature, null, null, null);
   }

   public void init() throws IllegalArgumentException, NoSuchMethodException, OseeArgumentException {
      mainMethod = findMainMethod(getMainMethodName());

      String[] parameterNames = getParameterNames();
      Class<?>[] parameterTypes = mainMethod.getParameterTypes();
      if (parameterNames.length != parameterTypes.length) {
         throw new OseeArgumentException("The method [%s] has %d parameters, but %d parameter names.",
            getMainMethodName(), parameterTypes.length, parameterNames.length);
      }

      parameters = new BlamParameter[parameterTypes.length];
      for (int i = 0; i < parameterTypes.length; i++) {
         parameters[i] = new BlamParameter(parameterNames[i], parameterTypes[i], null);
      }

   }

   public void executeOperation(Object[] actualParameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
      mainMethod.invoke(this, actualParameters);
   }

   private Method findMainMethod(String mainMethodName) throws IllegalArgumentException, NoSuchMethodException {
      Class<? extends DynamicBlamOperation> classRep = getClass();
      Method[] methods = classRep.getDeclaredMethods(); // get only the methods declared directly in the given class

      for (Method method : methods) {
         if (method.getName().equals(mainMethodName)) {
            return method;
         }
      }
      throw new NoSuchMethodException(mainMethodName);
   }

   /**
    * should return user oriented names for input values in the same order that they should be passed to the operation's
    * configured execution method
    * 
    * @return array of parameter names
    */
   public abstract String[] getParameterNames();

   public abstract String getMainMethodName();

   public BlamParameter[] getParameters() {
      return parameters;
   }
}