summaryrefslogtreecommitdiff
path: root/qpid/java/broker-codegen/src/main
diff options
context:
space:
mode:
authorRobert Godfrey <rgodfrey@apache.org>2014-04-30 01:22:13 +0000
committerRobert Godfrey <rgodfrey@apache.org>2014-04-30 01:22:13 +0000
commitae71aa7102a41735e49ec5c98409bc69fffd9a8f (patch)
treef0709ee8c5993d6995e5de59c0cdc855a540614e /qpid/java/broker-codegen/src/main
parent0d49f2fa419a414e1c9548001fcbde03d442f5c1 (diff)
downloadqpid-python-ae71aa7102a41735e49ec5c98409bc69fffd9a8f.tar.gz
QPID-5578 : [Java Broker] Use annotation to allow registration of all ConfiguredObject types at startup. Use this meta data for REST servlets. Remove unnecessary pluggable factory interfaces
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1591170 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker-codegen/src/main')
-rw-r--r--qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectFactoryGenerator.java5
-rw-r--r--qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectRegistrationGenerator.java180
-rw-r--r--qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ManagedObjectFactory.java30
-rw-r--r--qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor1
4 files changed, 183 insertions, 33 deletions
diff --git a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectFactoryGenerator.java b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectFactoryGenerator.java
index 658b468c3b..8fd5de76e5 100644
--- a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectFactoryGenerator.java
+++ b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectFactoryGenerator.java
@@ -23,8 +23,7 @@ package org.apache.qpid.server.model;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
-import java.util.Arrays;
-import java.util.HashSet;
+import java.util.Collections;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
@@ -54,7 +53,7 @@ public class ConfiguredObjectFactoryGenerator extends AbstractProcessor
@Override
public Set<String> getSupportedAnnotationTypes()
{
- return new HashSet<>(Arrays.asList(ManagedObjectFactory.class.getName(), ManagedObjectFactoryConstructor.class.getName()));
+ return Collections.singleton(ManagedObjectFactoryConstructor.class.getName());
}
@Override
diff --git a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectRegistrationGenerator.java b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectRegistrationGenerator.java
new file mode 100644
index 0000000000..48e9c960b2
--- /dev/null
+++ b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ConfiguredObjectRegistrationGenerator.java
@@ -0,0 +1,180 @@
+/*
+ *
+ * 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.qpid.server.model;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.RoundEnvironment;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.util.Elements;
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import org.apache.qpid.server.License;
+
+public class ConfiguredObjectRegistrationGenerator extends AbstractProcessor
+{
+
+ public static final String MANAGED_OBJECT_CANONICAL_NAME = "org.apache.qpid.server.model.ManagedObject";
+
+ private Map<String, Set<String>> _managedObjectClasses = new HashMap<>();
+
+ @Override
+ public SourceVersion getSupportedSourceVersion()
+ {
+ return SourceVersion.latest();
+ }
+
+ @Override
+ public Set<String> getSupportedAnnotationTypes()
+ {
+ return Collections.singleton(MANAGED_OBJECT_CANONICAL_NAME);
+ }
+
+ @Override
+ public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
+ {
+
+ Elements elementUtils = processingEnv.getElementUtils();
+ TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_OBJECT_CANONICAL_NAME);
+
+
+ try
+ {
+
+ for (Element e : roundEnv.getElementsAnnotatedWith(annotationElement))
+ {
+ if (e.getKind().equals(ElementKind.INTERFACE) || e.getKind().equals(ElementKind.CLASS))
+ {
+ PackageElement packageElement = elementUtils.getPackageOf(e);
+ String packageName = packageElement.getQualifiedName().toString();
+
+ Set<String> classNames = _managedObjectClasses.get(packageName);
+ if (classNames == null)
+ {
+ classNames = new HashSet<>();
+ _managedObjectClasses.put(packageName, classNames);
+ }
+ classNames.add(e.getSimpleName().toString());
+ }
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, e.getSimpleName());
+ }
+ for (Map.Entry<String, Set<String>> entry : _managedObjectClasses.entrySet())
+ {
+ generateRegistrationFile(entry.getKey(), entry.getValue());
+ }
+ _managedObjectClasses.clear();
+ }
+ catch (Exception e)
+ {
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error: " + e.getLocalizedMessage());
+ }
+
+ return false;
+ }
+
+ private void generateRegistrationFile(final String packageName, final Set<String> classNames)
+ {
+ final String className = "ConfiguredObjectRegistrationImpl";
+ final String qualifiedClassName = packageName + "." + className;
+
+ try
+ {
+
+
+ JavaFileObject factoryFile = processingEnv.getFiler().createSourceFile(qualifiedClassName);
+
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(factoryFile.openOutputStream(), "UTF-8"));
+ pw.println("/*");
+ for (String headerLine : License.LICENSE)
+ {
+ pw.println(" *" + headerLine);
+ }
+ pw.println(" */");
+ pw.println();
+ pw.print("package ");
+ pw.print(packageName);
+ pw.println(";");
+ pw.println();
+
+ pw.println("import java.util.Collections;");
+ pw.println("import java.util.HashSet;");
+ pw.println("import java.util.Set;");
+ pw.println();
+ pw.println("import org.apache.qpid.server.model.ConfiguredObject;");
+ pw.println("import org.apache.qpid.server.plugin.ConfiguredObjectRegistration;");
+ pw.println("import org.apache.qpid.server.plugin.PluggableService;");
+ pw.println();
+ pw.println("@PluggableService");
+ pw.println("public class " + className + " implements ConfiguredObjectRegistration");
+ pw.println("{");
+ pw.println(" private final Set<Class<? extends ConfiguredObject>> _implementations;");
+ pw.println();
+ pw.println(" public " + className + "()");
+ pw.println(" {");
+ pw.println(" Set<Class<? extends ConfiguredObject>> implementations = new HashSet<>();");
+ for(String implementationName : classNames)
+ {
+ pw.println(" implementations.add("+implementationName+".class);");
+ }
+ pw.println(" _implementations = Collections.unmodifiableSet(implementations);");
+ pw.println(" }");
+ pw.println();
+ pw.println(" public String getType()");
+ pw.println(" {");
+ pw.println(" return \""+packageName+"\";");
+ pw.println(" }");
+ pw.println();
+ pw.println(" public Set<Class<? extends ConfiguredObject>> getConfiguredObjectClasses()");
+ pw.println(" {");
+ pw.println(" return _implementations;");
+ pw.println(" }");
+ pw.println();
+
+
+ pw.println("}");
+
+ pw.close();
+ }
+ catch (IOException e)
+ {
+ processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+ "Failed to write file: "
+ + qualifiedClassName
+ + " - "
+ + e.getLocalizedMessage()
+ );
+ }
+ }
+
+}
diff --git a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ManagedObjectFactory.java b/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ManagedObjectFactory.java
deleted file mode 100644
index b74e25df7f..0000000000
--- a/qpid/java/broker-codegen/src/main/java/org/apache/qpid/server/model/ManagedObjectFactory.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- *
- * 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.qpid.server.model;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-@Retention(RetentionPolicy.SOURCE)
-
-public @interface ManagedObjectFactory
-{
-}
diff --git a/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor
index 3219c441a3..005394d209 100644
--- a/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor
+++ b/qpid/java/broker-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor
@@ -18,3 +18,4 @@
#
org.apache.qpid.server.model.ConfiguredObjectFactoryGenerator
org.apache.qpid.server.plugin.PluggableProcessor
+org.apache.qpid.server.model.ConfiguredObjectRegistrationGenerator