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/01/24 01:30:30 UTC
svn commit: r614753 - in /incubator/shindig/trunk/java/gadgets/src:
main/java/org/apache/shindig/gadgets/
main/java/org/apache/shindig/gadgets/http/
test/java/org/apache/shindig/gadgets/
Author: etnu
Date: Wed Jan 23 16:30:28 2008
New Revision: 614753
URL: http://svn.apache.org/viewvc?rev=614753&view=rev
Log:
Implemented https://issues.apache.org/jira/browse/SHINDIG-33 and marked Executor-only ctor as deprecated.
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServerConfig.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java?rev=614753&r1=614752&r2=614753&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java Wed Jan 23 16:30:28 2008
@@ -1,18 +1,24 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
*/
package org.apache.shindig.gadgets;
+import org.apache.shindig.util.Check;
+
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -41,10 +47,38 @@
private static final Logger logger
= Logger.getLogger("org.apache.shindig.gadgets");
+ /**
+ * Creates a GadgetServer without a config.
+ *
+ * @deprecated Replaced by {@link #GadgetServer(GadgetConfig)}.
+ * @param executor
+ */
+ @Deprecated
public GadgetServer(Executor executor) {
this.executor = executor;
}
+ /**
+ * Creates a GadgetServer using the provided configuration.
+ *
+ * @param config
+ * @throws IllegalArgumentException When missing required fields aren't set.
+ */
+ public GadgetServer(GadgetServerConfig config) {
+ executor = config.getExecutor();
+ Check.notNull(executor, "setExecutor is required.");
+ registry = config.getFeatureRegistry();
+ Check.notNull(registry, "setFeatureRegistry is required.");
+ specCache = config.getSpecCache();
+ Check.notNull(specCache, "setSpecCache is required.");
+ messageBundleCache = config.getMessageBundleCache();
+ Check.notNull(messageBundleCache, "setMessageBundleCache is required.");
+ fetcher = config.getContentFetcher();
+ Check.notNull(fetcher, "setContentFetcher is required.");
+
+ gadgetBlacklist = config.getGadgetBlacklist();
+ }
+
public void setSpecCache(GadgetDataCache<GadgetSpec> specCache) {
this.specCache = specCache;
}
@@ -67,6 +101,7 @@
/**
* Process a single gadget.
+ *
* @param gadgetId
* @param userPrefs
* @param locale
@@ -75,12 +110,13 @@
* @return The processed gadget.
* @throws GadgetProcessException
*/
- public Gadget processGadget(Gadget.ID gadgetId,
+ public Gadget processGadget(GadgetView.ID gadgetId,
UserPrefs userPrefs,
Locale locale,
RenderingContext rctx,
ProcessingOptions options)
throws GadgetProcessException {
+ // TODO: Remove dep checks when GadgetServer(Executor) is removed.
if (specCache == null) {
throw new GadgetProcessException(GadgetException.Code.MISSING_SPEC_CACHE);
}
@@ -288,7 +324,7 @@
if (wc.context.getOptions().ignoreCache) {
return;
}
-
+
GadgetSpec spec = specCache.get(gadgetId.getKey());
if (spec != null) {
wc.gadget = new Gadget(gadgetId, spec, prefs);
@@ -335,11 +371,11 @@
GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT,
"Malformed gadget spec URL: " + gadgetId.getURI().toString());
}
-
+
GadgetSpecParser specParser = new GadgetSpecParser();
GadgetSpec spec = specParser.parse(gadgetId, xml);
wc.gadget = new Gadget(gadgetId, spec, prefs);
-
+
// This isn't a separate job because if it is we'd just need another
// flag telling us not to store to the cache.
if (!wc.context.getOptions().ignoreCache) {
Added: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServerConfig.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServerConfig.java?rev=614753&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServerConfig.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServerConfig.java Wed Jan 23 16:30:28 2008
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.shindig.gadgets;
+
+import java.util.concurrent.Executor;
+
+/**
+ * Stores configuration data for a GadgetServer.
+ *
+ * The main purpose of this class is to allow for better readability of
+ * GadgetServer ctor parameters.
+ *
+ * Usage:
+ *
+ * <code>
+ * GadgetServer server = new GadgetServer(new GadgetServerConfig()
+ * .setRemoteContentFetcher(new BasicRemoteContentFetcher())
+ * .setGadgetCache(new BasicGadgetDataCache<GadgetSpec>())
+ * .setOtherProperties(...));
+ * </code>
+ *
+ * Any missing data will result in GadgetServer throwing an IllegalArgsException
+ * unless noted as "optional" here.
+ */
+public class GadgetServerConfig {
+
+ private Executor executor;
+
+ public Executor getExecutor() {
+ return executor;
+ }
+
+ public GadgetServerConfig setExecutor(Executor executor) {
+ this.executor = executor;
+ return this;
+ }
+
+ private GadgetFeatureRegistry featureRegistry;
+
+ public GadgetFeatureRegistry getFeatureRegistry() {
+ return featureRegistry;
+ }
+
+ public GadgetServerConfig setFeatureRegistry(GadgetFeatureRegistry featureRegistry) {
+ this.featureRegistry = featureRegistry;
+ return this;
+ }
+
+ private GadgetDataCache<GadgetSpec> specCache;
+
+ public GadgetDataCache<GadgetSpec> getSpecCache() {
+ return specCache;
+ }
+
+ public GadgetServerConfig setSpecCache(GadgetDataCache<GadgetSpec> specCache) {
+ this.specCache = specCache;
+ return this;
+ }
+
+ private GadgetDataCache<MessageBundle> messageBundleCache;
+
+ public GadgetDataCache<MessageBundle> getMessageBundleCache() {
+ return messageBundleCache;
+ }
+
+ public GadgetServerConfig setMessageBundleCache(GadgetDataCache<MessageBundle> mbCache) {
+ messageBundleCache = mbCache;
+ return this;
+ }
+
+ private RemoteContentFetcher contentFetcher;
+
+ public RemoteContentFetcher getContentFetcher() {
+ return contentFetcher;
+ }
+
+ public GadgetServerConfig setContentFetcher(RemoteContentFetcher contentFetcher) {
+ this.contentFetcher = contentFetcher;
+ return this;
+ }
+
+ // Optional
+ private GadgetBlacklist gadgetBlacklist;
+
+ public GadgetBlacklist getGadgetBlacklist() {
+ return gadgetBlacklist;
+ }
+
+ public GadgetServerConfig setGadgetBlacklist(GadgetBlacklist gadgetBlacklist) {
+ this.gadgetBlacklist = gadgetBlacklist;
+ return this;
+ }
+}
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java?rev=614753&r1=614752&r2=614753&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java Wed Jan 23 16:30:28 2008
@@ -20,6 +20,7 @@
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.GadgetFeatureRegistry;
import org.apache.shindig.gadgets.GadgetServer;
+import org.apache.shindig.gadgets.GadgetServerConfig;
import org.apache.shindig.gadgets.GadgetSpec;
import org.apache.shindig.gadgets.GadgetView;
import org.apache.shindig.gadgets.JsLibrary;
@@ -51,10 +52,9 @@
* Servlet for rendering Gadgets, typically in an IFRAME.
*/
public class GadgetRenderingServlet extends HttpServlet {
- private GadgetServer gadgetServer;
+ private final GadgetServer gadgetServer;
+ private final GadgetServerConfig serverConfig;
private String jsServicePath;
- private boolean usingCustomServer = false;
- private GadgetFeatureRegistry registry;
private static final String CAJA_PARAM = "caja";
private static final String USERPREF_PARAM_PREFIX = "up_";
private static final String LIBS_PARAM_NAME = "libs";
@@ -64,13 +64,19 @@
/**
* Creates a {@code GadgetRenderingServlet} with default executor,
* caches, etc.
+ *
+ * Note that features aren't loaded until init() is called.
+ *
+ * @throws GadgetException If something went wrong during configuration.
*/
- public GadgetRenderingServlet() {
- gadgetServer = new GadgetServer(Executors.newCachedThreadPool());
- gadgetServer.setMessageBundleCache(
- new BasicGadgetDataCache<MessageBundle>());
- gadgetServer.setSpecCache(new BasicGadgetDataCache<GadgetSpec>());
- gadgetServer.setContentFetcher(new BasicRemoteContentFetcher(1024 * 1024));
+ public GadgetRenderingServlet() throws GadgetException {
+ serverConfig = new GadgetServerConfig()
+ .setExecutor(Executors.newCachedThreadPool())
+ .setMessageBundleCache(new BasicGadgetDataCache<MessageBundle>())
+ .setSpecCache(new BasicGadgetDataCache<GadgetSpec>())
+ .setContentFetcher(new BasicRemoteContentFetcher(1024 * 1024))
+ .setFeatureRegistry(new GadgetFeatureRegistry(null));
+ gadgetServer = new GadgetServer(serverConfig);
}
/**
@@ -80,7 +86,9 @@
*/
public GadgetRenderingServlet(GadgetServer server) {
gadgetServer = server;
- usingCustomServer = true;
+ // Set this to null to indicate that all configuration has been done
+ // custom.
+ serverConfig = null;
}
@Override
@@ -92,11 +100,11 @@
jsPath = DEFAULT_JS_SERVICE_PATH;
}
jsServicePath = jsPath;
- if (!usingCustomServer) {
+ if (serverConfig != null) {
+ // Using the default server.
String features = context.getInitParameter("features");
try {
- registry = new GadgetFeatureRegistry(features);
- gadgetServer.setGadgetFeatureRegistry(registry);
+ serverConfig.getFeatureRegistry().registerFeatures(features);
} catch (GadgetException e) {
e.printStackTrace();
System.exit(1);
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java?rev=614753&r1=614752&r2=614753&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java Wed Jan 23 16:30:28 2008
@@ -1,19 +1,19 @@
-/*
- * $Id$
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
*
- * Copyright 2007 The Apache Software Foundation
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
*/
package org.apache.shindig.gadgets;
@@ -32,7 +32,7 @@
public class GadgetServerTest extends EasyMockTestCase {
final Executor executor = Executors.newCachedThreadPool();
- final GadgetServer gadgetServer = new GadgetServer(executor);
+ final GadgetServer gadgetServer;
final RemoteContentFetcher fetcher = mock(RemoteContentFetcher.class);
@SuppressWarnings(value="unchecked")
final GadgetDataCache<GadgetSpec> specCache = mock(GadgetDataCache.class);
@@ -40,11 +40,13 @@
final GadgetDataCache<MessageBundle> bundleCache = mock(GadgetDataCache.class);
public GadgetServerTest() throws GadgetException {
- GadgetFeatureRegistry registry = new GadgetFeatureRegistry(null);
- gadgetServer.setGadgetFeatureRegistry(registry);
- gadgetServer.setContentFetcher(fetcher);
- gadgetServer.setSpecCache(specCache);
- gadgetServer.setMessageBundleCache(bundleCache);
+ // TODO: need to test for configuration errors.
+ gadgetServer = new GadgetServer(new GadgetServerConfig()
+ .setExecutor(executor)
+ .setSpecCache(specCache)
+ .setMessageBundleCache(bundleCache)
+ .setFeatureRegistry(new GadgetFeatureRegistry(null))
+ .setContentFetcher(fetcher));
}
public void testGadgetSpecNotInCache() throws Exception {