1 package jp.sourceforge.stigmata;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.PrintWriter;
10 import java.net.MalformedURLException;
12 import java.net.URLClassLoader;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import jp.sourceforge.stigmata.event.BirthmarkEngineListener;
18 import jp.sourceforge.stigmata.printer.PrinterManager;
19 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
20 import jp.sourceforge.stigmata.ui.swing.ExtensionFilter;
21 import jp.sourceforge.stigmata.utils.ConfigFileExporter;
22 import jp.sourceforge.stigmata.utils.ConfigFileImporter;
26 * @author Haruaki Tamada
28 public class Stigmata{
30 * instance. singleton pattern.
32 private static Stigmata stigmata;
33 private PrinterManager manager = PrinterManager.getInstance();
34 private BirthmarkEnvironment defaultEnvironment;
35 private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
38 * private constructor.
45 * gets only instance of this class.
47 public static synchronized Stigmata getInstance(){
49 stigmata = new Stigmata();
55 * creates a new birthmark context.
57 public BirthmarkContext createContext(){
58 return new BirthmarkContext(createEnvironment());
62 * creates a new birthmark environment.
64 public BirthmarkEnvironment createEnvironment(){
65 return new BirthmarkEnvironment(defaultEnvironment);
69 * creates a new birthmark engine.
71 public BirthmarkEngine createEngine(){
72 return createEngine(createEnvironment());
76 * creates a new birthmark engine with given environment.
78 public BirthmarkEngine createEngine(BirthmarkEnvironment environment){
79 BirthmarkEngine engine = new BirthmarkEngine(environment);
80 for(BirthmarkEngineListener listener: listeners){
81 engine.addBirthmarkEngineListener(listener);
86 public PrinterManager getPrinterManager(){
90 public void addBirthmarkEngineListener(BirthmarkEngineListener listener){
91 listeners.add(listener);
94 public void removeBirthmarkEngineListener(BirthmarkEngineListener listener){
95 listeners.remove(listener);
98 public void configuration(){
99 configuration(null, false);
102 public void configuration(String filePath, boolean resetFlag){
103 InputStream target = null;
104 if(filePath != null){
106 target = new FileInputStream(filePath);
107 } catch(FileNotFoundException e){
112 if(filePath == null){
113 String currentDirectory = System.getProperty("execution.directory");
114 if(currentDirectory == null){
115 currentDirectory = System.getProperty("user.dir");
117 File file = new File(currentDirectory, "stigmata.xml");
119 file = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
126 target = new FileInputStream(file);
127 } catch (FileNotFoundException ex) {
128 // never throwed this exception;
129 throw new InternalError(ex.getMessage());
133 if(target == null || resetFlag){
134 target = getClass().getResourceAsStream("/resources/stigmata.xml");
136 defaultEnvironment = null;
137 BirthmarkEnvironment.resetSettings();
140 initConfiguration(target);
143 private void initConfiguration(InputStream in){
144 if(defaultEnvironment == null){
145 defaultEnvironment = BirthmarkEnvironment.getDefaultEnvironment();
147 buildStigmataDirectory(BirthmarkEnvironment.getStigmataHome());
149 defaultEnvironment.setClassLoader(buildClassLoader("plugins"));
151 ConfigFileImporter parser = new ConfigFileImporter(defaultEnvironment);
153 } catch(IOException e){
154 throw new ApplicationInitializationError(e);
156 for(Iterator<BirthmarkSpi> i = defaultEnvironment.lookupProviders(BirthmarkSpi.class); i.hasNext(); ){
157 BirthmarkSpi service = i.next();
158 defaultEnvironment.addService(service);
160 PrinterManager.refresh(defaultEnvironment);
161 exportConfigFile(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
164 private void buildStigmataDirectory(String homeDirectory){
165 File file = new File(homeDirectory);
166 if(file.exists() && file.isFile()){
167 File dest = new File(file.getParent(), ".stigmata.back");
173 File pluginDir = new File(file, "plugins");
174 if(!pluginDir.exists()){
179 private void exportConfigFile(String parent, String fileName){
181 File file = new File(parent, fileName);
183 ConfigFileExporter exporter = new ConfigFileExporter(defaultEnvironment);
184 exporter.export(new PrintWriter(new FileWriter(file)));
186 } catch(IOException e){
191 private static ClassLoader buildClassLoader(String path){
192 File directory = new File(BirthmarkEnvironment.getStigmataHome(), path);
193 File[] jarfiles = directory.listFiles(new ExtensionFilter("jar"));
195 if(jarfiles == null) jarfiles = new File[0];
197 URL[] urls = new URL[jarfiles.length];
198 for(int i = 0; i < jarfiles.length; i++){
199 urls[i] = jarfiles[i].toURI().toURL();
201 return new URLClassLoader(urls, Stigmata.class.getClassLoader());
202 } catch(MalformedURLException e){