OSDN Git Service

694f1da8c6759940405c5344f611d2cb293f14d1
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / result / history / ExtractedBirthmarkServiceManager.java
1 package jp.sourceforge.stigmata.result.history;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.LinkedHashSet;
10 import java.util.Map;
11 import java.util.Set;
12
13 import jp.sourceforge.stigmata.BirthmarkContext;
14 import jp.sourceforge.stigmata.BirthmarkEnvironment;
15 import jp.sourceforge.stigmata.BirthmarkStoreTarget;
16 import jp.sourceforge.stigmata.ExtractionResultSet;
17 import jp.sourceforge.stigmata.spi.ExtractedBirthmarkService;
18
19 /**
20  * 
21  * @author Haruaki Tamada
22  */
23 public class ExtractedBirthmarkServiceManager{
24     private ExtractedBirthmarkServiceManager parent;
25     private Map<BirthmarkStoreTarget, ExtractedBirthmarkService> targets = new HashMap<BirthmarkStoreTarget, ExtractedBirthmarkService>();
26     private BirthmarkEnvironment env;
27
28     public ExtractedBirthmarkServiceManager(BirthmarkEnvironment env){
29         this.env = env;
30         this.parent = null;
31     }
32
33     public ExtractedBirthmarkServiceManager(BirthmarkEnvironment env, ExtractedBirthmarkServiceManager parent){
34         this(env);
35         this.parent = parent;
36     }
37
38     public ExtractionResultSet createDefaultResultSet(BirthmarkContext context){
39         BirthmarkStoreTarget bst = context.getStoreTarget();
40         if(bst == null){
41             String type = env.getProperty("birthmark.store.target");
42             if(type == null){
43                 type = "XMLFILE";
44             }
45             bst = BirthmarkStoreTarget.valueOf(type);
46         }
47         if(bst == null){
48             bst = BirthmarkStoreTarget.XMLFILE;
49         }
50
51         ExtractedBirthmarkService service = findService(bst);
52
53         return service.createResultSet(context);
54     }
55
56     public ExtractedBirthmarkHistory getHistory(String id){
57         ExtractedBirthmarkHistory history = null;
58         if(parent != null){
59             history = parent.getHistory(id);
60         }
61         if(history == null){
62             int index = id.indexOf(":");
63             String type = id.substring(0, index);
64             BirthmarkStoreTarget bst = BirthmarkStoreTarget.valueOf(type);
65             String path = id.substring(index + 1);
66
67             ExtractedBirthmarkService service = findService(bst);
68             if(service != null){
69                 history = service.getHistory(path);
70             }
71         }
72         return history;
73     }
74
75     public synchronized String[] getHistoryIds(){
76         Set<String> values = new LinkedHashSet<String>();
77         if(parent != null){
78             for(String id: parent.getHistoryIds()){
79                 values.add(id);
80             }
81         }
82         addValuesFromProperty(values);
83         addValuesFromSystemFile(values);
84
85         char separator = File.separatorChar;
86         values.add(
87             "XMLFILE:" + BirthmarkEnvironment.getStigmataHome()
88             + separator + "extracted_birthmarks"
89         );
90         return values.toArray(new String[values.size()]);
91     }
92
93     private synchronized ExtractedBirthmarkService findService(BirthmarkStoreTarget bst){
94         ExtractedBirthmarkService spi = targets.get(bst);
95         if(spi == null){
96             refreshService();
97         }
98         spi = targets.get(bst);
99
100         return spi;
101     }
102
103     private synchronized void refreshService(){
104         for(Iterator<ExtractedBirthmarkService> i = env.lookupProviders(ExtractedBirthmarkService.class); i.hasNext(); ){
105             ExtractedBirthmarkService service = i.next();
106             targets.put(service.getTarget(), service);
107         }
108     }
109
110     private void addValuesFromSystemFile(Set<String> values){
111         File file = new File(BirthmarkEnvironment.getStigmataHome(), "storelocations.txt");
112         if(file.exists()){
113             try{
114                 BufferedReader in = new BufferedReader(new FileReader(file));
115                 String line;
116                 while((line = in.readLine()) != null){
117                     values.add(line);
118                 }
119             } catch(IOException e){
120             }
121         }
122     }
123
124     private void addValuesFromProperty(Set<String> values){
125         String path = env.getProperty("extracted.birthmark.store.locations");
126         if(path != null){
127             addValuesFromProperty(values);
128             String[] paths = path.split(", *");
129             for(String p: paths){
130                 values.add(p);
131             }
132         }
133     }
134 }