source: main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/ExtensionInformation.java@ 21919

Last change on this file since 21919 was 21919, checked in by sjm84, 14 years ago

New code for GAI extension manager

File size: 4.4 KB
Line 
1package org.greenstone.admin.guiext;
2
3import org.w3c.dom.Element;
4import org.w3c.dom.NodeList;
5
6import java.net.URLClassLoader;
7import java.net.URL;
8
9import java.lang.reflect.Method;
10import java.lang.reflect.Constructor;
11
12import java.io.File;
13
14import java.awt.BorderLayout;
15
16import javax.swing.JTextArea;
17import javax.swing.JPanel;
18
19import org.greenstone.admin.GAI;
20
21public class ExtensionInformation
22{
23 protected String _name = null;
24 protected String _group = null;
25 protected String _fileStem = null;
26 protected String _description = null;
27 protected String _baseExt = null;
28 protected String _baseURL = null;
29 protected SequenceList _sequenceList= null;
30 protected boolean _guiExtLoaded = false;
31
32 protected static JPanel _extPane = new JPanel();
33
34 public ExtensionInformation(Element extensionElement, String baseURL){
35 if(extensionElement != null){
36 _name = ExtXMLHelper.getValueFromSingleElement(extensionElement, ExtXMLHelper.NAME, true);
37 _group = ExtXMLHelper.getValueFromSingleElement(extensionElement, ExtXMLHelper.GROUP, true);
38 _fileStem = ExtXMLHelper.getValueFromSingleElement(extensionElement, ExtXMLHelper.FILE_STEM, true);
39 _description = ExtXMLHelper.getValueFromSingleElement(extensionElement, ExtXMLHelper.DESCRIPTION, true);
40 _baseExt = ExtXMLHelper.getValueFromSingleElement(extensionElement, ExtXMLHelper.BASE_EXT, true);
41 _baseURL = baseURL;
42 _sequenceList = new SequenceList(ExtXMLHelper.getSingleChildElement(extensionElement, ExtXMLHelper.SEQUENCE_LIST, true), this);
43 }
44 else{
45 System.err.println("<" + ExtXMLHelper.EXTENSION + "> element is null");
46 }
47 }
48
49 public String getBaseURL()
50 {
51 return _baseURL;
52 }
53
54 public String getName()
55 {
56 return _name;
57 }
58
59 public String getGroup()
60 {
61 return _group;
62 }
63
64 public String getFileStem()
65 {
66 return _fileStem;
67 }
68
69 public String getDescription()
70 {
71 return _description;
72 }
73
74 public String getBaseClassName()
75 {
76 return _baseExt;
77 }
78
79 public SequenceList getSequenceList()
80 {
81 return _sequenceList;
82 }
83
84 public Object getExtObject()
85 {
86 loadGuiExtFile();
87 Class extClass = null;
88 try{
89 extClass = Class.forName(_baseExt);
90 }
91 catch(Exception ex){
92 System.err.println("Could not create the extension class used for callback methods, either the class name is incorrect or the class does not exist inside the guiext.jar file");
93 }
94 Constructor classConstructor = null;
95 Object classObj = null;
96
97 try{
98 classConstructor = extClass.getConstructor(new Class[0]);
99 classObj = classConstructor.newInstance(new Object[0]);
100 }
101 catch(Exception ex){
102 ex.printStackTrace();
103 System.err.println("Could not create the extension class used for callback methods, either the class name is incorrect or the class does not exist inside the guiext.jar file");
104 return null;
105 }
106
107 return classObj;
108 }
109
110 public void loadExternalJar(String filename){
111 //Load the dynamic jar loader
112 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
113 Class sysclass = URLClassLoader.class;
114 final Class[] parameters = new Class[]{URL.class};
115 Method method = null;
116 try{
117 method = sysclass.getDeclaredMethod("addURL", parameters);
118 }
119 catch(Exception ex){
120 ex.printStackTrace();
121 }
122 method.setAccessible(true);
123
124 File externalFile = new File(filename);
125 if(!externalFile.exists()){
126 System.err.println("Cannot load the external jar file at " + filename + " as it does not exist");
127 return;
128 }
129
130 try {
131 URL u = new URL(externalFile.toURI().toURL().toString());
132 method.invoke(sysloader, new Object[]{u});
133 } catch (Throwable t) {
134 t.printStackTrace();
135 }
136
137 _guiExtLoaded = true;
138 }
139
140 public void loadGuiExtFile()
141 {
142 if(!_guiExtLoaded){
143 loadExternalJar(getExtensionDirectory() + System.getProperty("file.separator") + "guiext.jar");
144 }
145 }
146
147 public boolean isGuiExtLoaded()
148 {
149 return _guiExtLoaded;
150 }
151
152 public void changeExtPane(JPanel extPane)
153 {
154 _extPane.removeAll();
155 _extPane.add(extPane, BorderLayout.CENTER);
156 _extPane.revalidate();
157 _extPane.repaint();
158 }
159
160 public static void setExtPane(JPanel extPane)
161 {
162 _extPane = extPane;
163 }
164
165 public JPanel getExtPane()
166 {
167 return _extPane;
168 }
169
170 public String getExtensionDirectory()
171 {
172 return GAI.getGSDL3ExtensionHome() + System.getProperty("file.separator") + _fileStem;
173 }
174}
Note: See TracBrowser for help on using the repository browser.