OSDN Git Service

remove Revision tag
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / utils / HermesUtility.java
1 package jp.sourceforge.stigmata.utils;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.net.MalformedURLException;
14 import java.net.URL;
15
16 import jp.sourceforge.stigmata.BirthmarkEnvironment;
17 import jp.sourceforge.talisman.hermes.Hermes;
18 import jp.sourceforge.talisman.hermes.HermesContext;
19 import jp.sourceforge.talisman.hermes.HermesContextExporter;
20 import jp.sourceforge.talisman.hermes.HermesContextParser;
21 import jp.sourceforge.talisman.hermes.HermesException;
22 import jp.sourceforge.talisman.hermes.InvalidHermesConfigException;
23 import jp.sourceforge.talisman.hermes.maven.Artifact;
24
25 /**
26  * 
27  * @author Haruaki Tamada
28  */
29 public class HermesUtility{
30     private HermesContext context;
31     private Hermes hermes;
32     private Artifact[] updateTargets;
33
34     public HermesUtility(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
35         loadHermesContext(env);
36     }
37
38     public HermesUtility(){
39     }
40
41     public void updateContext(BirthmarkEnvironment env) throws IOException{
42         String path = env.getProperty("location.hermes.config");
43         OutputStream out = null;
44         if(path != null && path.startsWith("file:")){
45             out = new URL(path).openConnection().getOutputStream();
46         }
47         else{
48             File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
49             out = new FileOutputStream(file);
50         }
51         HermesContextExporter exporter = new HermesContextExporter();
52         exporter.export(out, context);
53         if(out != null){
54             out.close();
55         }
56     }
57
58     public void loadHermesContext(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
59         HermesContextParser parser = new HermesContextParser();
60         InputStream in = getInputStream(env);
61         context = parser.parse(in);
62         if(in != null){
63             try{
64                 in.close();
65             } catch(IOException e){
66                 // ignore exception.
67             }
68         }
69         if(context.getDestination().contains("${stigmata.home}")){
70             String dest = context.getDestination();
71             dest = dest.replace("${stigmata.home}", BirthmarkEnvironment.getStigmataHome());
72             context.setDestination(dest);
73         }
74
75         hermes = new Hermes(context);
76     }
77
78     public boolean canUpdate() throws IOException, HermesException{
79         return getUpdateTarget().length > 0;
80     }
81
82     public Artifact getCurrentArtifact(String groupId, String artifactId){
83         return context.getDependency(groupId, artifactId);
84     }
85
86     public Artifact[] getUpdateTarget() throws IOException, HermesException{
87         if(updateTargets == null){
88             updateTargets = hermes.getUpdateTarget();
89         }
90         return updateTargets;
91     }
92
93     public void update() throws IOException, HermesException{
94         hermes.update();
95         updateTargets = null;
96     }
97
98     public Hermes getHermes(){
99         if(hermes == null){
100             throw new IllegalStateException("call loadHermesContext first!");
101         }
102         return hermes;
103     }
104
105     private InputStream getInputStream(BirthmarkEnvironment env) throws IOException{
106         InputStream in = null;
107         if(env.getProperty("location.hermes.config") != null){
108             try{
109                 URL url = new URL(env.getProperty("location.hermes.config"));
110                 in = url.openStream();
111             } catch(MalformedURLException e){
112                 // ignore exception.
113             }
114         }
115         else{
116             File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
117             if(!file.exists()){
118                 copyFile(getClass().getResource("/resources/hermes.xml"), file);
119             }
120
121             in = new FileInputStream(file);
122         }
123         return in;
124     }
125
126     private void copyFile(URL from, File to) throws IOException{
127         InputStream in = null;
128         OutputStream out = null;
129         try{
130             in = from.openStream();
131             out = new FileOutputStream(to);
132             byte[] data = new byte[256];
133             int read = 0;
134             while((read = in.read(data)) != -1){
135                 out.write(data, 0, read);
136             }
137         } finally{
138             if(in != null){
139                 in.close();
140             }
141             if(out != null){
142                 out.close();
143             }
144         }
145     }
146 }