OSDN Git Service

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