001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id:$
023: */
024: package com.bostechcorp.cbesb.ui.util.ucm;
025:
026: import groovy.lang.GroovyClassLoader;
027:
028: import java.io.File;
029: import java.lang.reflect.Method;
030: import java.net.URLClassLoader;
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: import org.eclipse.core.resources.IProject;
035: import org.eclipse.core.resources.ResourcesPlugin;
036: import org.eclipse.swt.widgets.Text;
037:
038: import com.bostechcorp.cbesb.ui.util.MsgUtil;
039:
040: public class GroovyCommenMethod {
041: private static GroovyCommenMethod instance = null;
042:
043: private GroovyCommenMethod() {
044: }
045:
046: public static GroovyCommenMethod getInstance() {
047: if (instance == null)
048: instance = new GroovyCommenMethod();
049: return instance;
050: }
051:
052: public void getGroovyClass(File scriptFolder, List<String> list) {
053: if (scriptFolder.exists() && scriptFolder.isDirectory()) {
054: for (File file : scriptFolder.listFiles()) {
055: if (file.isFile() && !file.isHidden()) {
056: if (file.getName().substring(
057: file.getName().lastIndexOf(".") + 1,
058: file.getName().length()).toLowerCase()
059: .endsWith("groovy"))
060: list.add(file.getName());
061: }
062: }
063: } else {
064: MsgUtil
065: .warningMsg("Not found groovy class in the project.");
066: return;
067: }
068: }
069:
070: public void selectMethod(String saName, Text text, String className) {
071:
072: String projectName = saName;
073: String scriptClassName = className;
074: if (className.indexOf("::") > -1) {
075:
076: String[] array = className.split("::");
077: String temp;
078: if (array.length > 2) {
079: projectName = array[1];
080: temp = array[2];
081: } else {
082: temp = array[1];
083:
084: }
085: int pos = temp.lastIndexOf('/');
086: scriptClassName = temp.substring(pos + 1);
087:
088: }
089: try {
090: List<String> methodList = getMethodList(projectName,
091: scriptClassName);
092:
093: if (methodList.size() != 0) {
094: UcmSelectionDialog dialog = new UcmSelectionDialog(text
095: .getShell(), methodList, "Method", text);
096: dialog.open();
097: } else {
098: MsgUtil
099: .warningMsg("Not found method in the class.\nPlease build the project first.");
100: }
101:
102: } catch (Exception e) {
103:
104: MsgUtil.warningMsg(e.getMessage() + " cann't be load!");
105: e.printStackTrace();
106: }
107:
108: }
109:
110: private List<String> getMethodList(String projectName,
111: String className) throws Exception {
112: IProject project = ResourcesPlugin.getWorkspace().getRoot()
113: .getProject(projectName);
114: File file = project.getFolder("src/script/" + className)
115: .getLocation().toFile();
116: List<String> list = new ArrayList<String>();
117: if (file.exists()) {
118: URLClassLoader urlClassLoader = new URLClassLoader(
119: UcmClassURLHelper.getURL(projectName, project
120: .getLocation().toOSString()), this
121: .getClass().getClassLoader());
122: GroovyClassLoader classLoader = new GroovyClassLoader(
123: urlClassLoader);
124:
125: classLoader.addClasspath(file.getParent());
126: int pos = className.indexOf(".");
127: Class groovyClass = classLoader.loadClass(className
128: .substring(0, pos), true, false);
129:
130: for (Method subMethod : groovyClass.getMethods()) {
131: list.add(subMethod.getName());
132: }
133:
134: }
135: return list;
136:
137: }
138: }
|