View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: Configuration.java
13   * Created: 2007-02-01
14   * Author: fox
15   * $Id: Configuration.java 3296 2009-09-02 13:13:14Z tmilos $
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   * CAST configuration utility class.
31   *
32   * @author AGH CAST Team
33   */
34  public final class Configuration {
35  
36  	/*
37  	 * UNUSED <2009-01-13> [tmilos] is this used?
38  	 */
39  
40  	/**
41  	 * Diagram file extension.
42  	 */
43  	public static final String FILE_EXTENSION = "bil"; //$NON-NLS-1$
44  
45  	/**
46  	 * Project file extension.
47  	 */
48  	public static final String DUMMY_PROJECT_FILE_EXTENSION = "link"; //$NON-NLS-1$
49  
50  	/**
51  	 * Sample diagram file prefix.
52  	 */
53  	public static final String SAMPLE_FILE_PREFIX = "diagram"; //$NON-NLS-1$
54  
55  	/**
56  	 * Sample diagram file.
57  	 */
58  	public static final String SAMPLE_FILE_NAME = SAMPLE_FILE_PREFIX + "." + //$NON-NLS-1$
59  	        FILE_EXTENSION;
60  
61  	public static final String BUNDLE_CLASS_NAME = "org.osgi.framework.Bundle"; //$NON-NLS-1$
62  
63  	public static final String PLATFORM_CLASS_NAME = "org.eclipse.core.runtime.Platform"; //$NON-NLS-1$
64  
65  	public static final String PATH_CLASS_NAME = "org.eclipse.core.runtime.Path"; //$NON-NLS-1$
66  
67  	public static final String IPATH_CLASS_NAME = "org.eclipse.core.runtime.IPath"; //$NON-NLS-1$
68  
69  	/**
70  	 * Log4J configuration file.
71  	 */
72  	public static final String LOG_CONFIG_FILE = "./log4j.properties"; //$NON-NLS-1$
73  
74  	/**
75  	 * Template constants (cast data plug-in contains template implementation).
76  	 */
77  	public static final String TEMPLATE_FILE_EXTENSION = "tmpl"; //$NON-NLS-1$
78  
79  	public static final String TEMPLATES_PROJECT_NAME = ".templates"; //$NON-NLS-1$
80  
81  	private Configuration() {
82  		URL basePath = Configuration.getBaseUrl(Activator.PLUGIN_ID);
83  		try {
84  			// logger configuration
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  	 * {@link Configuration} singleton holder class.
95  	 *
96  	 * @author AGH CAST Team
97  	 */
98  	private static final class ConfigurationHolder {
99  
100 		/**
101 		 * Holder for singleton of {@link Configuration}.
102 		 */
103 		static final Configuration INSTANCE = new Configuration();
104 
105 	}
106 
107 	public static Configuration getInstance() {
108 		return ConfigurationHolder.INSTANCE;
109 	}
110 
111 	/**
112 	 * Returns plug-in base URL.
113 	 *
114 	 * @param pluginID
115 	 *            plug-in Id
116 	 * @return base URL of the plug-in
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", //$NON-NLS-1$
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[] { "./" }); //$NON-NLS-1$
141 
142 			findMethod = platformClass.getMethod("find", new Class[] { //$NON-NLS-1$
143 			        bundleClass, ipathClass });
144 			baseUrl = (URL)findMethod.invoke(null, new Object[] { bundle, path });
145 
146 			asLocalMethod = platformClass.getMethod("asLocalURL", new Class[] { //$NON-NLS-1$
147 			        URL.class });
148 			baseUrl = (URL)asLocalMethod.invoke(null, new Object[] { baseUrl });
149 
150 			System.err.println("Base URL: " + baseUrl); //$NON-NLS-1$
151 		} catch (Throwable ex) {
152 			ex.printStackTrace();
153 			System.err.println(Messages.Configuration_13);
154 			return null;
155 		}
156 		return baseUrl;
157 	}
158 }