1 package jp.sourceforge.stigmata;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStreamWriter;
10 import java.io.PrintWriter;
11 import java.net.MalformedURLException;
13 import java.net.URLClassLoader;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import jp.sourceforge.stigmata.event.BirthmarkEngineListener;
19 import jp.sourceforge.stigmata.printer.PrinterManager;
20 import jp.sourceforge.stigmata.spi.BirthmarkService;
21 import jp.sourceforge.stigmata.ui.swing.ExtensionFilter;
22 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
23 import jp.sourceforge.stigmata.utils.ConfigFileImporter;
27 * @author Haruaki Tamada
29 public class Stigmata{
31 * instance. singleton pattern.
33 private static Stigmata stigmata;
34 private PrinterManager manager = PrinterManager.getInstance();
35 private BirthmarkEnvironment defaultEnvironment;
36 private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
39 * private constructor.
46 * gets only instance of this class.
48 public static synchronized Stigmata getInstance(){
50 stigmata = new Stigmata();
56 * creates a new birthmark context.
58 public BirthmarkContext createContext(){
59 return new BirthmarkContext(createEnvironment());
63 * creates a new birthmark environment.
65 public BirthmarkEnvironment createEnvironment(){
66 return new BirthmarkEnvironment(defaultEnvironment);
70 * creates a new birthmark engine.
72 public BirthmarkEngine createEngine(){
73 return createEngine(createEnvironment());
77 * creates a new birthmark engine with given environment.
79 public BirthmarkEngine createEngine(BirthmarkEnvironment environment){
80 BirthmarkEngine engine = new BirthmarkEngine(environment);
81 for(BirthmarkEngineListener listener: listeners){
82 engine.addBirthmarkEngineListener(listener);
87 public PrinterManager getPrinterManager(){
91 public void addBirthmarkEngineListener(BirthmarkEngineListener listener){
92 listeners.add(listener);
95 public void removeBirthmarkEngineListener(BirthmarkEngineListener listener){
96 listeners.remove(listener);
99 public void configuration(){
100 configuration(null, false);
103 public void configuration(String filePath, boolean resetFlag){
104 InputStream target = null;
105 if(filePath != null){
107 target = new FileInputStream(filePath);
108 } catch(FileNotFoundException e){
113 if(filePath == null){
114 String currentDirectory = System.getProperty("execution.directory");
115 if(currentDirectory == null){
116 currentDirectory = System.getProperty("user.dir");
118 File file = new File(currentDirectory, "stigmata.xml");
120 file = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
127 target = new FileInputStream(file);
128 } catch (FileNotFoundException ex) {
129 // never throwed this exception;
130 throw new InternalError(ex.getMessage());
134 if(target == null || resetFlag){
135 target = getClass().getResourceAsStream("/resources/stigmata.xml");
137 defaultEnvironment = null;
138 BirthmarkEnvironment.resetSettings();
142 initConfiguration(target);
147 } catch(IOException e){
153 private void initConfiguration(InputStream in){
154 if(defaultEnvironment == null){
155 defaultEnvironment = BirthmarkEnvironment.getDefaultEnvironment();
157 buildStigmataDirectory(BirthmarkEnvironment.getStigmataHome());
159 defaultEnvironment.setClassLoader(buildClassLoader("plugins"));
161 ConfigFileImporter parser = new ConfigFileImporter(defaultEnvironment);
163 } catch(IOException e){
164 throw new ApplicationInitializationError(e);
166 for(Iterator<BirthmarkService> i = defaultEnvironment.lookupProviders(BirthmarkService.class); i.hasNext(); ){
167 BirthmarkService service = i.next();
168 defaultEnvironment.addService(service);
170 PrinterManager.refresh(defaultEnvironment);
171 exportConfigFile(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
174 private void buildStigmataDirectory(String homeDirectory){
175 File file = new File(homeDirectory);
176 if(file.exists() && file.isFile()){
177 File dest = new File(file.getParent(), ".stigmata.back");
183 File pluginDir = new File(file, "plugins");
184 if(!pluginDir.exists()){
189 private void exportConfigFile(String parent, String fileName){
191 File file = new File(parent, fileName);
193 ConfigFileExporter exporter = new ConfigFileExporter(defaultEnvironment);
194 String encoding = defaultEnvironment.getProperty("encoding.config");
195 if(encoding == null){
196 encoding = defaultEnvironment.getProperty("encoding");
198 if(encoding == null){
201 exporter.export(new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)));
203 } catch(IOException e){
208 private static ClassLoader buildClassLoader(String path){
209 File directory = new File(BirthmarkEnvironment.getStigmataHome(), path);
210 File[] jarfiles = directory.listFiles(new ExtensionFilter("jar"));
212 if(jarfiles == null) jarfiles = new File[0];
214 URL[] urls = new URL[jarfiles.length];
215 for(int i = 0; i < jarfiles.length; i++){
216 urls[i] = jarfiles[i].toURI().toURL();
218 return new URLClassLoader(urls, Stigmata.class.getClassLoader());
219 } catch(MalformedURLException e){