OSDN Git Service

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