OSDN Git Service

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