View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui;
7   
8   import net.kwfgrid.gwes.restfulclient.RestfulStructureGWES;
9   import net.kwfgrid.gworkflowdl.protocol.service.IStructureService;
10  import net.kwfgrid.gwui.servicestubs.GraphVizWS;
11  import org.apache.log4j.Logger;
12  import org.apache.log4j.Level;
13  import org.glassbox.SwingThread;
14  import org.glassbox.gui.AbstractGroup;
15  
16  import java.io.*;
17  
18  /***
19     The GWUI context.
20   */
21  public class GWUI extends AbstractGroup {
22      /*** The key for the gwes-uri property. */
23      public static final String GWES_URI_KEY = "service.gwes.uri";
24      /*** The key for the graphviz-uri property. */
25      public static final String GRAPHVIZ_URI_KEY = "service.graphviz.uri";
26      /*** The key for the xsd-path property. */
27      public static final String XSD_PATH = "gworkflowdl.xsd.path";
28      /*** The key for the user-id property. */
29      public static final String USER_ID_KEY = "user.id";
30      /*** The key for the workflow-id property. */
31      public static final String WORKFLOW_ID_KEY = "workflow.id";
32      /*** The key for the uaa-frame property. */
33      public static final String UAA_FRAME_KEY = "portlet.uaa.frame";
34      /*** The key for the uaa-portlet-url property. */
35      public static final String UAA_PORTLET_URL_KEY = "portlet.uaa.url";
36      /*** The key for the applet-context-workflowidlisteners property */
37      public static final String APPLET_CONTEXT_WORKFLOWIDLISTENERS_KEY = "applet.context.workflowidlisteners";
38      /*** The key for the applet-context-tasklistapplet property */
39      public static final String APPLET_CONTEXT_TASKLISTAPPLET_KEY = "applet.context.tasklistapplet";
40      /*** The key for the WSClient logger level property. */
41      public static final String SERVICESTUBS_PACKAGE = "net.kwfgrid.gwui.servicestubs";
42      public static final String SERVICESTUBS_LOGGER_LEVEL_KEY = "log4j.logger."+SERVICESTUBS_PACKAGE;
43      /*** The key for the GWESCLIENT logger level property. */
44      public static final String GWESCLIENT_PACKAGE = "net.kwfgrid.gwes.restfulclient";
45      public static final String GWESCLIENT_LOGGER_LEVEL_KEY = "log4j.logger."+GWESCLIENT_PACKAGE;
46      /*** The key for the form-selection-servlet URL property. */
47      public static final String FORM_SELECTION_SERVLET_URL_KEY = "servlet.form-selection.url";
48  
49      public static final String TOKEN_FILE_CHOOSER = "token-context";
50      public static final String WORKFLOW_FILE_CHOOSER = "workflow-context";
51  
52      private static final Logger logger = Logger.getLogger(GWUI.class);
53  
54      private static GWUI _instance;
55  
56      /***
57         Singleton
58      */
59      private GWUI() {
60  	super();
61  	// _gwes = new HashMap();
62  	// _nullgwes = null;
63      }
64  
65      public static synchronized GWUI getInstance() {
66  	if (_instance==null) _instance=new GWUI();
67  	return _instance;
68      }
69  
70      public void setProperty(final String key, final Object value) {
71  	logger.debug("setProperty("+key+", "+value+")");
72  
73  	SwingThread.invokeAndWaitSilently(new Runnable() {
74  		public void run() { GWUI.super.setProperty(key, value); }
75  	    });
76  
77  	logger.debug("setProperty.exit");
78      }
79  
80      publicong> static void setLoggerLevel(String packageName, String level) {
81          if (level != null) {
82              Logger myLogger = Logger.getRootLogger();
83              >if (packageName != null) {
84                  myLogger = Logger.getLogger(packageName);
85              }
86              if ("DEBUG".equalsIgnoreCase(level)) {
87                  myLogger.setLevel(Level.DEBUG);
88              } else if("INFO".equalsIgnoreCase(level)) {
89                  myLogger.setLevel(Level.INFO);
90              } else if("WARN".equalsIgnoreCase(level)) {
91                  myLogger.setLevel(Level.WARN);
92              } else if("ERROR".equalsIgnoreCase(level)) {
93                  myLogger.setLevel(Level.ERROR);
94              }
95          }
96      }
97  
98      /***
99         Get an instance of the stub of the GraphViz WebService for the specified workflow-id.
100      */
101     public GraphVizWS getGraphVizWS(String workflowid) {
102 	return new GraphVizWS((String)getProperty(GRAPHVIZ_URI_KEY));
103     }
104     
105     /***
106        Get an instance of the stub of the GWES.
107      */
108     public net.kwfgrid.gwes.GWES getGWES() throws Exception {
109     	return RestfulStructureGWES.getInstance((String)getProperty(GWES_URI_KEY));
110     }
111 
112     public IStructureService getIStructureService() throws Exception {
113         return RestfulStructureGWES.getInstance((String)getProperty(GWES_URI_KEY));
114     }
115 
116     /***
117      * Read a file and put the contents to a string
118      *
119      * @param fileName File name of the file to read
120      * @return String with the contents of the file
121      */
122     public static String readFile(String fileName) throws IOException {
123         logger.debug("Reading file " + fileName + "...");
124 
125         StringBuffer buffer = new StringBuffer();
126         BufferedReader fileReader;
127 	fileReader = new BufferedReader(new FileReader(fileName));
128 	
129         String line;
130 	line = fileReader.readLine();
131 	while (line != null) {
132 	    buffer.append(line);
133 	    buffer.append('\n');
134 	    line = fileReader.readLine();
135 	}
136 
137         return buffer.toString();
138     }
139 
140     /***
141        Write string content to a file.
142      */
143     public static void writeFile(File file, String content) throws IOException {
144 	logger.debug("Writing file "+file+"...");
145 
146 	PrintWriter writer = new PrintWriter(new FileWriter(file));
147 	writer.print(content);
148 	writer.flush();
149 	writer.close();
150     }
151 }