OSDN Git Service

organized imports
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / Stigmata.java
1 package jp.sourceforge.stigmata;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
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;
15 import java.net.URL;
16 import java.net.URLClassLoader;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20
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;
27
28 /**
29  * 
30  * @author Haruaki Tamada
31  * @version $Revision$ 
32  */
33 public class Stigmata{
34     /**
35      * instance. singleton pattern.
36      */
37     private static Stigmata stigmata;
38     private BirthmarkEnvironment defaultEnvironment;
39     private List<BirthmarkEngineListener> listeners = new ArrayList<BirthmarkEngineListener>();
40
41     /**
42      * private constructor.
43      */
44     private Stigmata(){
45         configuration();
46     }
47
48     /**
49      * gets only instance of this class.
50      */
51     public static synchronized Stigmata getInstance(){
52         if(stigmata == null){
53             stigmata = new Stigmata();
54         }
55         return stigmata;
56     }
57
58     /**
59      * creates a new birthmark context.
60      */
61     public BirthmarkContext createContext(){
62         return new BirthmarkContext(createEnvironment());
63     }
64
65     /**
66      * creates a new birthmark environment.
67      */
68     public BirthmarkEnvironment createEnvironment(){
69         return new BirthmarkEnvironment(defaultEnvironment);
70     }
71
72     /**
73      * creates a new birthmark engine.
74      */
75     public BirthmarkEngine createEngine(){
76         return createEngine(createEnvironment());
77     }
78
79     /**
80      * creates a new birthmark engine with given environment.
81      */
82     public BirthmarkEngine createEngine(BirthmarkEnvironment environment){
83         BirthmarkEngine engine = new BirthmarkEngine(environment);
84         for(BirthmarkEngineListener listener: listeners){
85             engine.addBirthmarkEngineListener(listener);
86         }
87         return engine;
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.updateServices(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 }