OSDN Git Service

Delete Subversion Tags (Revision, Id)
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / Stigmata.java
1 package jp.sourceforge.stigmata;
2
3 import java.io.File;
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;
11 import java.net.URL;
12 import java.net.URLClassLoader;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
23
24 /**
25  * 
26  * @author Haruaki Tamada
27  */
28 public class Stigmata{
29     /**
30      * instance. singleton pattern.
31      */
32     private static Stigmata stigmata;
33     private PrinterManager manager = PrinterManager.getInstance();
34     private BirthmarkEnvironment defaultEnvironment;
35     private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
36
37     /**
38      * private constructor.
39      */
40     private Stigmata(){
41         configuration();
42     }
43
44     /**
45      * gets only instance of this class.
46      */
47     public static synchronized Stigmata getInstance(){
48         if(stigmata == null){
49             stigmata = new Stigmata();
50         }
51         return stigmata;
52     }
53
54     /**
55      * creates a new birthmark context.
56      */
57     public BirthmarkContext createContext(){
58         return new BirthmarkContext(createEnvironment());
59     }
60
61     /**
62      * creates a new birthmark environment.
63      */
64     public BirthmarkEnvironment createEnvironment(){
65         return new BirthmarkEnvironment(defaultEnvironment);
66     }
67
68     /**
69      * creates a new birthmark engine.
70      */
71     public BirthmarkEngine createEngine(){
72         return createEngine(createEnvironment());
73     }
74
75     /**
76      * creates a new birthmark engine with given environment.
77      */
78     public BirthmarkEngine createEngine(BirthmarkEnvironment environment){
79         BirthmarkEngine engine = new BirthmarkEngine(environment);
80         for(BirthmarkEngineListener listener: listeners){
81             engine.addBirthmarkEngineListener(listener);
82         }
83         return engine;
84     }
85
86     public PrinterManager getPrinterManager(){
87         return manager;
88     }
89
90     public void addBirthmarkEngineListener(BirthmarkEngineListener listener){
91         listeners.add(listener);
92     }
93
94     public void removeBirthmarkEngineListener(BirthmarkEngineListener listener){
95         listeners.remove(listener);
96     }
97
98     public void configuration(){
99         configuration(null, false);
100     }
101
102     public void configuration(String filePath, boolean resetFlag){
103         InputStream target = null;
104         if(filePath != null){
105             try{
106                 target = new FileInputStream(filePath);
107             } catch(FileNotFoundException e){
108                 filePath = null;
109             }
110         }
111
112         if(filePath == null){
113             String currentDirectory = System.getProperty("execution.directory");
114             if(currentDirectory == null){
115                 currentDirectory = System.getProperty("user.dir");
116             }
117             File file = new File(currentDirectory, "stigmata.xml");
118             if(!file.exists()){
119                 file = new File(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
120                 if(!file.exists()){
121                     file = null;
122                 }
123             }
124             if(file != null){
125                 try {
126                     target = new FileInputStream(file);
127                 } catch (FileNotFoundException ex) {
128                     // never throwed this exception;
129                     throw new InternalError(ex.getMessage());
130                 }
131             }
132         }
133         if(target == null || resetFlag){
134             target = getClass().getResourceAsStream("/resources/stigmata.xml");
135             if(resetFlag){
136                 defaultEnvironment = null;
137                 BirthmarkEnvironment.resetSettings();
138             }
139         }
140         initConfiguration(target);
141     }
142
143     private void initConfiguration(InputStream in){
144         if(defaultEnvironment == null){
145             defaultEnvironment = BirthmarkEnvironment.getDefaultEnvironment();
146         }
147         buildStigmataDirectory(BirthmarkEnvironment.getStigmataHome());
148
149         defaultEnvironment.setClassLoader(buildClassLoader("plugins"));
150         try{
151             ConfigFileImporter parser = new ConfigFileImporter(defaultEnvironment);
152             parser.parse(in);
153         } catch(IOException e){
154             throw new ApplicationInitializationError(e);
155         }
156         for(Iterator<BirthmarkSpi> i = defaultEnvironment.lookupProviders(BirthmarkSpi.class); i.hasNext(); ){
157             BirthmarkSpi service = i.next();
158             defaultEnvironment.addService(service);
159         }
160         PrinterManager.refresh(defaultEnvironment);
161         exportConfigFile(BirthmarkEnvironment.getStigmataHome(), "stigmata.xml");
162     }
163
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");
168             file.renameTo(dest);
169         }
170         if(!file.exists()){
171             file.mkdirs();
172         }
173         File pluginDir = new File(file, "plugins");
174         if(!pluginDir.exists()){
175             pluginDir.mkdirs();
176         }
177     }
178
179     private void exportConfigFile(String parent, String fileName){
180         try{
181             File file = new File(parent, fileName);
182             if(!file.exists()){
183                 ConfigFileExporter exporter = new ConfigFileExporter(defaultEnvironment);
184                 exporter.export(new PrintWriter(new FileWriter(file)));
185             }
186         } catch(IOException e){
187             e.printStackTrace();
188         }
189     }
190
191     private static ClassLoader buildClassLoader(String path){
192         File directory = new File(BirthmarkEnvironment.getStigmataHome(), path);
193         File[] jarfiles = directory.listFiles(new ExtensionFilter("jar"));
194
195         if(jarfiles == null) jarfiles = new File[0];
196         try{
197             URL[] urls = new URL[jarfiles.length];
198             for(int i = 0; i < jarfiles.length; i++){
199                 urls[i] = jarfiles[i].toURI().toURL();
200             }
201             return new URLClassLoader(urls, Stigmata.class.getClassLoader());
202         } catch(MalformedURLException e){
203         }
204         return null;
205     }
206 }