1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.util;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import org.apache.log4j.PropertyConfigurator;
26
27 import pl.edu.agh.cast.Activator;
28
29
30
31
32
33
34 public final class Configuration {
35
36
37
38
39
40
41
42
43 public static final String FILE_EXTENSION = "bil";
44
45
46
47
48 public static final String DUMMY_PROJECT_FILE_EXTENSION = "link";
49
50
51
52
53 public static final String SAMPLE_FILE_PREFIX = "diagram";
54
55
56
57
58 public static final String SAMPLE_FILE_NAME = SAMPLE_FILE_PREFIX + "." +
59 FILE_EXTENSION;
60
61 public static final String BUNDLE_CLASS_NAME = "org.osgi.framework.Bundle";
62
63 public static final String PLATFORM_CLASS_NAME = "org.eclipse.core.runtime.Platform";
64
65 public static final String PATH_CLASS_NAME = "org.eclipse.core.runtime.Path";
66
67 public static final String IPATH_CLASS_NAME = "org.eclipse.core.runtime.IPath";
68
69
70
71
72 public static final String LOG_CONFIG_FILE = "./log4j.properties";
73
74
75
76
77 public static final String TEMPLATE_FILE_EXTENSION = "tmpl";
78
79 public static final String TEMPLATES_PROJECT_NAME = ".templates";
80
81 private Configuration() {
82 URL basePath = Configuration.getBaseUrl(Activator.PLUGIN_ID);
83 try {
84
85 URL loggerConfigUrl = new URL(basePath, LOG_CONFIG_FILE);
86 PropertyConfigurator.configure(loggerConfigUrl);
87
88 } catch (MalformedURLException ex1) {
89 System.err.println(Messages.Configuration_8 + ex1);
90 }
91 }
92
93
94
95
96
97
98 private static final class ConfigurationHolder {
99
100
101
102
103 static final Configuration INSTANCE = new Configuration();
104
105 }
106
107 public static Configuration getInstance() {
108 return ConfigurationHolder.INSTANCE;
109 }
110
111
112
113
114
115
116
117
118 public static URL getBaseUrl(String pluginID) {
119 URL baseUrl = null;
120 Class<? extends Object> pathClass;
121 Class<? extends Object> ipathClass;
122 Class<? extends Object> platformClass;
123 Class<? extends Object> bundleClass;
124 Method getBundleMethod;
125 Method findMethod;
126 Method asLocalMethod;
127 Constructor<?> pathConstructor;
128 try {
129 bundleClass = Class.forName(BUNDLE_CLASS_NAME);
130 platformClass = Class.forName(PLATFORM_CLASS_NAME);
131
132 getBundleMethod = platformClass.getMethod("getBundle",
133 new Class[] { String.class });
134
135 Object bundle = getBundleMethod.invoke(null, new Object[] { pluginID });
136
137 pathClass = Class.forName(PATH_CLASS_NAME);
138 ipathClass = Class.forName(IPATH_CLASS_NAME);
139 pathConstructor = pathClass.getConstructor(new Class[] { String.class });
140 Object path = pathConstructor.newInstance(new Object[] { "./" });
141
142 findMethod = platformClass.getMethod("find", new Class[] {
143 bundleClass, ipathClass });
144 baseUrl = (URL)findMethod.invoke(null, new Object[] { bundle, path });
145
146 asLocalMethod = platformClass.getMethod("asLocalURL", new Class[] {
147 URL.class });
148 baseUrl = (URL)asLocalMethod.invoke(null, new Object[] { baseUrl });
149
150 System.err.println("Base URL: " + baseUrl);
151 } catch (Throwable ex) {
152 ex.printStackTrace();
153 System.err.println(Messages.Configuration_13);
154 return null;
155 }
156 return baseUrl;
157 }
158 }