OSDN Git Service

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