1 package jp.sourceforge.stigmata.utils;
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;
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;
23 * @author Haruaki Tamada
25 public class HermesUtility{
26 private HermesContext context;
27 private Hermes hermes;
28 private Artifact[] updateTargets;
30 public HermesUtility(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
31 loadHermesContext(env);
34 public HermesUtility(){
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();
44 File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
45 out = new FileOutputStream(file);
47 HermesContextExporter exporter = new HermesContextExporter();
48 exporter.export(out, context);
54 public void loadHermesContext(BirthmarkEnvironment env) throws IOException, InvalidHermesConfigException{
55 HermesContextParser parser = new HermesContextParser();
56 InputStream in = getInputStream(env);
57 context = parser.parse(in);
61 } catch(IOException e){
65 if(context.getDestination().contains("${stigmata.home}")){
66 String dest = context.getDestination();
67 dest = dest.replace("${stigmata.home}", BirthmarkEnvironment.getStigmataHome());
68 context.setDestination(dest);
71 hermes = new Hermes(context);
74 public boolean canUpdate() throws IOException, HermesException{
75 return getUpdateTarget().length > 0;
78 public Artifact getCurrentArtifact(String groupId, String artifactId){
79 return context.getDependency(groupId, artifactId);
82 public Artifact[] getUpdateTarget() throws IOException, HermesException{
83 if(updateTargets == null){
84 updateTargets = hermes.getUpdateTarget();
89 public void update() throws IOException, HermesException{
94 public Hermes getHermes(){
96 throw new IllegalStateException("call loadHermesContext first!");
101 private InputStream getInputStream(BirthmarkEnvironment env) throws IOException{
102 InputStream in = null;
103 if(env.getProperty("location.hermes.config") != null){
105 URL url = new URL(env.getProperty("location.hermes.config"));
106 in = url.openStream();
107 } catch(MalformedURLException e){
112 File file = new File(BirthmarkEnvironment.getStigmataHome(), "plugins/hermes.xml");
114 copyFile(getClass().getResource("/resources/hermes.xml"), file);
117 in = new FileInputStream(file);
122 private void copyFile(URL from, File to) throws IOException{
123 InputStream in = null;
124 OutputStream out = null;
126 in = from.openStream();
127 out = new FileOutputStream(to);
128 byte[] data = new byte[256];
130 while((read = in.read(data)) != -1){
131 out.write(data, 0, read);