1 package jp.sourceforge.stigmata.utils;
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;
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;
27 * @author Haruaki Tamada
29 public class HermesUtility{
30 private HermesContext context;
31 private Hermes hermes;
32 private Artifact[] updateTargets;
34 public HermesUtility(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
35 loadHermesContext(env);
38 public HermesUtility(){
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();
48 File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
49 out = new FileOutputStream(file);
51 HermesContextExporter exporter = new HermesContextExporter();
52 exporter.export(out, context);
58 public void loadHermesContext(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
59 HermesContextParser parser = new HermesContextParser();
60 InputStream in = getInputStream(env);
61 context = parser.parse(in);
65 } catch(IOException e){
69 if(context.getDestination().contains("${stigmata.home}")){
70 String dest = context.getDestination();
71 dest = dest.replace("${stigmata.home}", BirthmarkEnvironment.getStigmataHome());
72 context.setDestination(dest);
75 hermes = new Hermes(context);
78 public boolean canUpdate() throws IOException, HermesException{
79 return getUpdateTarget().length > 0;
82 public Artifact getCurrentArtifact(String groupId, String artifactId){
83 return context.getDependency(groupId, artifactId);
86 public Artifact[] getUpdateTarget() throws IOException, HermesException{
87 if(updateTargets == null){
88 updateTargets = hermes.getUpdateTarget();
93 public void update() throws IOException, HermesException{
98 public Hermes getHermes(){
100 throw new IllegalStateException("call loadHermesContext first!");
105 private InputStream getInputStream(BirthmarkEnvironment env) throws IOException{
106 InputStream in = null;
107 if(env.getProperty("location.hermes.config") != null){
109 URL url = new URL(env.getProperty("location.hermes.config"));
110 in = url.openStream();
111 } catch(MalformedURLException e){
116 File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
118 copyFile(getClass().getResource("/resources/hermes.xml"), file);
121 in = new FileInputStream(file);
126 private void copyFile(URL from, File to) throws IOException{
127 InputStream in = null;
128 OutputStream out = null;
130 in = from.openStream();
131 out = new FileOutputStream(to);
132 byte[] data = new byte[256];
134 while((read = in.read(data)) != -1){
135 out.write(data, 0, read);