View Javadoc

1   /***
2    * Created on Aug 5, 2005, Copyright UC Regents
3    */
4   package wise2.converter.converters;
5   
6   import java.io.ByteArrayInputStream;
7   import java.io.ByteArrayOutputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.net.URL;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.Map;
14  import java.util.Properties;
15  import java.util.logging.Logger;
16  import java.util.zip.ZipEntry;
17  import java.util.zip.ZipInputStream;
18  
19  import net.sf.sail.core.beans.Pod;
20  import net.sf.sail.core.entity.Rim;
21  
22  import org.telscenter.pas.beans.PasStep;
23  import org.telscenter.pas.pedagogica.PedagogicaStep;
24  
25  import wise2.converter.util.HTMLUtils;
26  
27  /***
28   * @author turadg
29   */
30  public class ConcordModelSaveJarConverter extends AbstractHtmlStepConverter {
31  	/***
32  	 * Logger for this class
33  	 */
34  	private static final Logger logger = Logger
35  			.getLogger(ConcordModelSaveJarConverter.class.getName());
36  
37  	private PedagogicaStep step;
38  	private HashMap rimMap = new HashMap();
39  
40  	@Override
41  	protected PasStep getTypedPasStep() {
42  		step = new PedagogicaStep();
43  		setUrlToIntroductionHtml(step);
44  		String saveJarName = stepNode.selectSingleNode("parameters/saveJar")
45  				.getText();
46  
47  		try {
48  			InputStream upload = uploadProvider.getProjectUpload(saveJarName);
49  			URL saveJarUrl = addToArchive(saveJarName, upload);
50  			step.setUrl(saveJarUrl);
51  			loadPropertyFiles(saveJarName);
52  		} catch (Exception e) {
53  			logger.severe("exception: " + e); //$NON-NLS-1$
54  		}
55  
56  		String html = stepNode.selectSingleNode("parameters/htmlIntro")
57  				.getText();
58  
59  		html = HTMLUtils.tidyHtml(html);
60  
61  		step.setIntroductionHtml(html);
62  
63  		String codebase = stepNode.selectSingleNode("parameters/codebase")
64  				.getText();
65  		step.setCodeBase(codebase);
66  
67  		// TODO write test launching class that provides necessary classes as
68  		// provided by JWS curnit extension
69  
70  		return step;
71  	}
72  
73  	protected Class getShape(Object value) {
74  		Class shape = String.class;
75  		// The value here will either be either a class name or
76  		// a Java-style variable name. If the latter, this method
77  		// should always return String.class
78  		String className = shape.getName();
79  		if (value instanceof String) {
80  			try {
81  				className = (String) value;
82  				// Make sure the string is non-empty and has one or more "." in it
83  				// as a regular class name would. Otherwise it will be assumed that
84  				// it is a variable name. Default package classes can't be used for
85  				// shapes in this case.
86  				shape = Class.forName(className);
87  			} catch (ClassNotFoundException e) {
88  				logger.warning("Could not find rim shape " + className);
89  			}
90  		}
91  		return shape;
92  	}
93  
94  	public void checkImportedClasses(Properties properties) {
95  		// separate out the string of imported classes...importedClasses is a
96  		// string of file names separated by ";"
97  		String importedClassesString = properties
98  				.getProperty("imported.classes");
99  		String patternString = ";";
100 		String[] importedClasses = importedClassesString.split(patternString);
101 
102 		// try to load each imported classes and throw an error if a class
103 		// cannot be loaded.
104 		String className = null;
105 		for (int i = 0; i < importedClasses.length; i++) {
106 			try {
107 				className = importedClasses[i];
108 				Class.forName(className);
109 			} catch (ClassNotFoundException e) {
110 				logger.severe("Cannot load imported class: " + className);
111 				e.printStackTrace();
112 			}
113 		}
114 	}
115 
116 	public void createRims(Properties properties) {
117 		Iterator rimEntries = properties.entrySet().iterator();
118 		Pod pod = getPod();
119 		Map rimMap = step.getRimMap();
120 		while (rimEntries.hasNext()) {
121 			Map.Entry rimEntry = (Map.Entry) rimEntries.next();
122 			String rimName = (String) rimEntry.getKey();
123 			Rim rim = new Rim();
124 			rim.setName(rimName);
125 			rim.setShape(getShape(rimEntry.getValue()));
126 			step.setRim(rimName, rim);
127 			pod.add(rim);
128 		}
129 		step.setRimMap(rimMap);
130 		pod.add(rimMap);
131 	}
132 
133 	/***
134 	 * Verify that all imported classes in Pedagogica state jar are available on
135 	 * the static classpath
136 	 * 
137 	 * @param saveJarName
138 	 *            name of the jar file
139 	 * 
140 	 */
141 	public void loadPropertyFiles(String saveJarName) {
142 		try {
143 			InputStream input = uploadProvider.getProjectUpload(saveJarName);
144 			ZipInputStream zipInput = new ZipInputStream(input);
145 			Properties properties = getProperties(zipInput, "classes/state.properties");
146 			properties.list(System.out);
147 			createRims(properties);
148 			
149 			input = uploadProvider.getProjectUpload(saveJarName);
150 			zipInput = new ZipInputStream(input);
151 			properties = getProperties(zipInput, "classes/classes.properties");
152 			properties.list(System.out);
153 			checkImportedClasses(properties);
154 		} catch (IOException e) {
155 			logger.severe("Could not load properties files from " + saveJarName);
156 			return;
157 		}
158 	}
159 
160 	/***
161 	 * Returns Properties [] object of the *.properties files in the jar
162 	 * @param input ZipInputStream object to look inside
163 	 * @param path String [] of path names
164 	 * @return Properties [] representation of *.properties data
165 	 */
166 	public Properties getProperties(ZipInputStream input, String path) {
167 		Properties props = new Properties();
168 		try {
169 			ZipEntry entry = input.getNextEntry();
170 			while (entry != null) {
171 				if (entry.getName().equals(path)) {
172 					InputStream byteStream = getByteInputStream(input, entry);
173 					if (byteStream != null)
174 						props.load(byteStream);
175 				}
176 				entry = input.getNextEntry();
177 			}
178 		} catch (IOException ioe) {
179 
180 		}
181 		return props;
182 	}
183 
184 	public InputStream getByteInputStream(ZipInputStream input, ZipEntry entry) {
185 		byte[] buffer = new byte[1024];
186 		ByteArrayOutputStream out = new ByteArrayOutputStream();
187 		try {
188 			int n = input.read(buffer, 0, 1024);
189 			while (n > 0) {
190 				out.write(buffer, 0, n);
191 				n = input.read(buffer, 0, 1024);
192 			}
193 			byte[] bytes = out.toByteArray();
194 			return new ByteArrayInputStream(bytes);
195 		} catch (IOException e) {
196 			// TODO Auto-generated catch block
197 			e.printStackTrace();
198 		}
199 		return null;
200 	}
201 }