You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/05/25 05:23:22 UTC
svn commit: r659901 - in /incubator/shindig/trunk/java/gadgets/src:
main/java/org/apache/shindig/gadgets/JsFeatureLoader.java
test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java
Author: etnu
Date: Sat May 24 20:23:21 2008
New Revision: 659901
URL: http://svn.apache.org/viewvc?rev=659901&view=rev
Log:
Improved JsFeatureLoader to take multiple files on input, largely eliminating the need to call loadFeatures directly. This uses a comma as a file separator rather than a colon due to a conflict with file prefixes. Future code changes to simplify GadgetFeatureLoader are warranted.
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsFeatureLoader.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsFeatureLoader.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsFeatureLoader.java?rev=659901&r1=659900&r2=659901&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsFeatureLoader.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsFeatureLoader.java Sat May 24 20:23:21 2008
@@ -22,6 +22,7 @@
import org.apache.shindig.common.xml.XmlUtil;
import org.apache.shindig.gadgets.http.HttpFetcher;
+import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
@@ -48,6 +49,8 @@
*/
public class JsFeatureLoader {
+ public final static char FILE_SEPARATOR = ',';
+
private final HttpFetcher fetcher;
private static final Logger logger
@@ -63,26 +66,28 @@
* be loaded. If res://*.txt is passed, we will look for named resources
* in the text file. If path is prefixed with res://, the file
* is treated as a resource, and all references are assumed to be
- * resources as well.
- * @throws GadgetException
+ * resources as well. Multiple locations may be specified by separating them with a comma.
+ * @throws GadgetException If any of the files can't be read.
*/
public void loadFeatures(String path, GadgetFeatureRegistry registry)
throws GadgetException {
List<ParsedFeature> features = new LinkedList<ParsedFeature>();
try {
- if (path.startsWith("res://")) {
- path = path.substring(6);
- logger.info("Loading resources from: " + path);
- if (path.endsWith(".txt")) {
- loadResources(ResourceLoader.getContent(path).split("[\r\n]+"),
- features);
+ for (String location : StringUtils.split(path, FILE_SEPARATOR)) {
+ if (location.startsWith("res://")) {
+ location = location.substring(6);
+ logger.info("Loading resources from: " + location);
+ if (location.endsWith(".txt")) {
+ loadResources(ResourceLoader.getContent(location).split("[\r\n]+"),
+ features);
+ } else {
+ loadResources(new String[]{location}, features);
+ }
} else {
- loadResources(new String[]{path}, features);
+ logger.info("Loading files from: " + location);
+ File file = new File(location);
+ loadFiles(new File[]{file}, features);
}
- } else {
- logger.info("Loading files from: " + path);
- File file = new File(path);
- loadFiles(new File[]{file}, features);
}
} catch (IOException e) {
throw new GadgetException(GadgetException.Code.INVALID_PATH, e);
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java?rev=659901&r1=659900&r2=659901&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/JsFeatureLoaderTest.java Sat May 24 20:23:21 2008
@@ -20,19 +20,21 @@
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
-import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.http.HttpRequest;
+import org.apache.shindig.gadgets.http.HttpResponse;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.URI;
import java.util.List;
+import java.util.Map;
public class JsFeatureLoaderTest extends GadgetTestFixture {
JsFeatureLoader loader;
private static final String FEATURE_NAME = "test";
+ private static final String ALT_FEATURE_NAME = "test2";
private static final String DEF_JS_CONTENT = "var hello = 'world';";
private static final String ALT_JS_CONTENT = "function test(){while(true);}";
private static final String CONT_A = "test";
@@ -45,6 +47,11 @@
loader = new JsFeatureLoader(fetcher);
}
+ private JsLibrary getJsLib(GadgetFeatureRegistry.Entry entry) {
+ GadgetFeature feature = entry.getFeature().create();
+ return feature.getJsLibraries(new GadgetContext()).get(0);
+ }
+
public void testBasicLoading() throws Exception {
String xml = "<feature>" +
" <name>" + FEATURE_NAME + "</name>" +
@@ -120,6 +127,39 @@
assertEquals(ALT_JS_CONTENT, libs.get(0).getContent());
assertEquals(FEATURE_NAME, libs.get(0).getFeature());
}
+
+ private File makeFeatureFile(String name, String content) throws Exception {
+ String xml = "<feature>" +
+ " <name>" + name + "</name>" +
+ " <gadget>" +
+ " <script>" + content + "</script>" +
+ " </gadget>" +
+ "</feature>";
+ File file = File.createTempFile(getName(), name + ".xml");
+ file.deleteOnExit();
+ BufferedWriter out = new BufferedWriter(new FileWriter(file));
+ out.write(xml);
+ out.close();
+ return file;
+ }
+
+ public void testMultiplePaths() throws Exception {
+ File file1 = makeFeatureFile(FEATURE_NAME, DEF_JS_CONTENT);
+ File file2 = makeFeatureFile(ALT_FEATURE_NAME, ALT_JS_CONTENT);
+
+ loader.loadFeatures(file1.getAbsolutePath() +
+ JsFeatureLoader.FILE_SEPARATOR +
+ file2.getAbsolutePath(), registry);
+ // TODO: This is too fragile. GadgetFeatureRegistry needs to be fixed.
+ Map<String, GadgetFeatureRegistry.Entry> entries
+ = registry.getAllFeatures();
+
+ JsLibrary lib1 = getJsLib(entries.get(FEATURE_NAME));
+ assertEquals(DEF_JS_CONTENT, lib1.getContent());
+
+ JsLibrary lib2 = getJsLib(entries.get(ALT_FEATURE_NAME));
+ assertEquals(ALT_JS_CONTENT, lib2.getContent());
+ }
}
class ContainerContext extends GadgetContext {