From e416b6615af1bc2b2e52dc34b7bda7421b054de2 Mon Sep 17 00:00:00 2001 From: lresende Date: Fri, 3 Apr 2009 08:18:49 +0000 Subject: TUSCANY-2878 - JavaScript Generator extension point. Used by implementation.widget to allow multiple implementations of js generator (e.g. tuscany specific and dojo based) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@761561 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/core/web/ComponentJavaScriptGenerator.java | 59 ++++++++ ...ComponentJavaScriptGeneratorExtensionPoint.java | 56 ++++++++ ...ComponentJavaScriptGeneratorExtensionPoint.java | 148 +++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGenerator.java create mode 100644 branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGeneratorExtensionPoint.java create mode 100644 branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/DefaultComponentJavaScriptGeneratorExtensionPoint.java (limited to 'branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core') diff --git a/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGenerator.java b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGenerator.java new file mode 100644 index 0000000000..2f9ba42bc4 --- /dev/null +++ b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGenerator.java @@ -0,0 +1,59 @@ +/* + * 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.tuscany.sca.core.web; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.runtime.RuntimeComponent; + + +/** + * Widget component script generator interface + * This generates the necessary JavaScript client code into a single JavaScript per component + * + * @version $Rev$ $Date$ + */ +public interface ComponentJavaScriptGenerator { + + /** + * Return the QName that identify the Component script generator + * This is used to identify different generators supporting different JavaScript frameworks + * + * @return The QName + */ + QName getQName(); + + /** + * Generate the Java Script code for a given component + * - generate/append client proxyies as needed + * - generate tuscany namespace as needed + * - generate properties for JavaScript injection + * - generate references for JavaScript injection + * + * @param component The SCA Component to be used + * @param pw A Print Writer where the script should be written to + * @throws IOException + */ + void generateJavaScriptCode(RuntimeComponent component, PrintWriter pw) throws IOException; + +} diff --git a/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGeneratorExtensionPoint.java b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGeneratorExtensionPoint.java new file mode 100644 index 0000000000..c2cba59362 --- /dev/null +++ b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/ComponentJavaScriptGeneratorExtensionPoint.java @@ -0,0 +1,56 @@ +/* + * 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.tuscany.sca.core.web; + +import java.util.List; + +import javax.xml.namespace.QName; + +/** + * Widget component script generator extension pointinterface + * + * @version $Rev$ $Date$ + */ +public interface ComponentJavaScriptGeneratorExtensionPoint { + + /** + * Add a component script generator + * @param componentScriptGenerator + */ + void addComponentJavaScriptGenerator(ComponentJavaScriptGenerator componentScriptGenerator); + + /** + * Remove a component script generator + * @param componentScriptGenerator + */ + void removeComponentJavaScriptGenerator(ComponentJavaScriptGenerator componentScriptGenerator); + + /** + * Get a component scrpt generator instance based on QName + * @param bindingName + * @return + */ + ComponentJavaScriptGenerator getComponentJavaScriptGenerator(QName bindingName); + + /** + * Get a list of component script generator instances + */ + List getComponentJavaScriptGenerators(); +} diff --git a/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/DefaultComponentJavaScriptGeneratorExtensionPoint.java b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/DefaultComponentJavaScriptGeneratorExtensionPoint.java new file mode 100644 index 0000000000..85b1a7aead --- /dev/null +++ b/branches/sca-java-1.x/modules/core-web/src/main/java/org/apache/tuscany/sca/core/web/DefaultComponentJavaScriptGeneratorExtensionPoint.java @@ -0,0 +1,148 @@ +/* + * 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.tuscany.sca.core.web; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceDiscovery; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +public class DefaultComponentJavaScriptGeneratorExtensionPoint implements ComponentJavaScriptGeneratorExtensionPoint { + private final List generators = new ArrayList(); + private final Map generatorsByQName = new HashMap(); + + private ExtensionPointRegistry extensionPoints; + private Monitor monitor = null; + + private boolean loaded = false; + + public DefaultComponentJavaScriptGeneratorExtensionPoint(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + if (monitorFactory != null) { + this.monitor = monitorFactory.createMonitor(); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = new ProblemImpl(this.getClass().getName(), "core-web-validation-messages", Severity.ERROR, model, message, ex); + monitor.problem(problem); + } + } + + public void addComponentJavaScriptGenerator(ComponentJavaScriptGenerator componentScriptGenerator) { + if (componentScriptGenerator.getQName() != null) { + generatorsByQName.put(componentScriptGenerator.getQName(), componentScriptGenerator); + } + + generators.add(componentScriptGenerator); + } + + public void removeComponentJavaScriptGenerator(ComponentJavaScriptGenerator componentScriptGenerator) { + if (componentScriptGenerator.getQName() != null) { + generatorsByQName.remove(componentScriptGenerator.getQName()); + } + + generators.remove(componentScriptGenerator); + } + + + public ComponentJavaScriptGenerator getComponentJavaScriptGenerator(QName bindingName) { + loadFacories(); + return generatorsByQName.get(bindingName); + } + + public List getComponentJavaScriptGenerators() { + loadFacories(); + return this.generators; + } + + /** + * Private Utility methods + */ + + + /** + * Lazily load artifact processors registered in the extension point. + */ + @SuppressWarnings("unchecked") + private synchronized void loadFacories() { + if (loaded) { + return; + } + + // Get the proxy factories declarations + Set factoryDeclarations = null; + try { + factoryDeclarations = ServiceDiscovery.getInstance().getServiceDeclarations(ComponentJavaScriptGenerator.class); + } catch (IOException e) { + IllegalStateException ie = new IllegalStateException(e); + error("IllegalStateException", factoryDeclarations, ie); + throw ie; + } + + for (ServiceDeclaration processorDeclaration : factoryDeclarations) { + // Create a factory, and register it + ComponentJavaScriptGenerator generator = null; + try { + Class generatorClass = processorDeclaration.loadClass(); + + Constructor constructor = generatorClass.getConstructor(ExtensionPointRegistry.class); + generator = constructor.newInstance(extensionPoints); + + } catch (Exception e) { + IllegalStateException ie = new IllegalStateException(e); + error("IllegalStateException", generator, ie); + throw ie; + } + + addComponentJavaScriptGenerator(generator); + } + + loaded = true; + } + + +} -- cgit v1.2.3