1 package jp.sourceforge.stigmata;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.FileWriter;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.PrintWriter;
14 import java.net.MalformedURLException;
16 import java.net.URLClassLoader;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
21 import jp.sourceforge.stigmata.event.BirthmarkEngineListener;
22 import jp.sourceforge.stigmata.printer.PrinterManager;
23 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
24 import jp.sourceforge.stigmata.ui.swing.ExtensionFilter;
25 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
26 import jp.sourceforge.stigmata.utils.ConfigFileImporter;
30 * @author Haruaki Tamada
33 public class Stigmata{
35 * instance. singleton pattern.
37 private static Stigmata stigmata;
38 private PrinterManager manager = PrinterManager.getInstance();
39 private BirthmarkEnvironment defaultEnvironment;
40 private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
43 * private constructor.
50 * gets only instance of this class.
52 public static synchronized Stigmata getInstance(){
54 stigmata = new Stigmata();
60 * creates a new birthmark context.
62 public BirthmarkContext createContext(){
63 return new BirthmarkContext(createEnvironment());
67 * creates a new birthmark environment.
69 public BirthmarkEnvironment createEnvironment(){
70 return new BirthmarkEnvironment(defaultEnvironment);
74 * creates a new birthmark engine.
76 public BirthmarkEngine createEngine(){
77 return createEngine(createEnvironment());
81 * creates a new birthmark engine with given environment.
83 public BirthmarkEngine createEngine(BirthmarkEnvironment environment){
84 BirthmarkEngine engine = new BirthmarkEngine(environment);
85 for(BirthmarkEngineListener listener: listeners){
86 engine.addBirthmarkEngineListener(listener);
91 public PrinterManager getPrinterManager(){
95 public void addBirthmarkEngineListener(BirthmarkEngineListener listener){
96 listeners.add(listener);
99 public void removeBirthmarkEngineListener(BirthmarkEngineListener listener){
100 listeners.remove(listener);
103 public void configuration(){
104 configuration(null, false);
107 public void configuration(String filePath, boolean resetFlag){
108 InputStream target = null;
109 if(filePath != null){
111 target = new FileInputStream(filePath);
112 } catch(FileNotFoundException e){
117 if(filePath == null){
118 String currentDirectory = System.getProperty("execution.directory");
119 if(currentDirectory == null){
120 currentDirectory = System.getProperty("user.dir");
122 File file = new File(currentDirectory, "stigmata.xml");
124 file = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
131 target = new FileInputStream(file);
132 } catch (FileNotFoundException ex) {
133 // never throwed this exception;
134 throw new InternalError(ex.getMessage());
138 if(target == null || resetFlag){
139 target = getClass().getResourceAsStream("/resources/stigmata.xml");
141 defaultEnvironment = null;
142 BirthmarkEnvironment.resetSettings();
145 initConfiguration(target);
148 private void initConfiguration(InputStream in){
149 if(defaultEnvironment == null){
150 defaultEnvironment = BirthmarkEnvironment.getDefaultEnvironment();
152 buildStigmataDirectory(BirthmarkEnvironment.getStigmataHome());
154 defaultEnvironment.setClassLoader(buildClassLoader("plugins"));
156 ConfigFileImporter parser = new ConfigFileImporter(defaultEnvironment);
158 } catch(IOException e){
159 throw new ApplicationInitializationError(e);
161 for(Iterator<BirthmarkSpi> i = defaultEnvironment.lookupProviders(BirthmarkSpi.class); i.hasNext(); ){
162 BirthmarkSpi service = i.next();
163 defaultEnvironment.addService(service);
165 PrinterManager.updateServices(defaultEnvironment);
166 exportConfigFile(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
169 private void buildStigmataDirectory(String homeDirectory){
170 File file = new File(homeDirectory);
171 if(file.exists() && file.isFile()){
172 File dest = new File(file.getParent(), ".stigmata.back");
178 File pluginDir = new File(file, "plugins");
179 if(!pluginDir.exists()){
184 private void exportConfigFile(String parent, String fileName){
186 File file = new File(parent, fileName);
188 ConfigFileExporter exporter = new ConfigFileExporter(defaultEnvironment);
189 exporter.export(new PrintWriter(new FileWriter(file)));
191 } catch(IOException e){
196 private static ClassLoader buildClassLoader(String path){
197 File directory = new File(BirthmarkEnvironment.getStigmataHome(), path);
198 File[] jarfiles = directory.listFiles(new ExtensionFilter("jar"));
200 if(jarfiles == null) jarfiles = new File[0];
202 URL[] urls = new URL[jarfiles.length];
203 for(int i = 0; i < jarfiles.length; i++){
204 urls[i] = jarfiles[i].toURI().toURL();
206 return new URLClassLoader(urls, Stigmata.class.getClassLoader());
207 } catch(MalformedURLException e){