diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedOperation.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedOperation.java new file mode 100644 index 0000000..f31e2dc --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedOperation.java @@ -0,0 +1,74 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.enterprise.util.Nonbinding; + +import br.gov.frameworkdemoiselle.DemoiselleException; + +/** + *

Indicates that a method is a managed operation, meaning you can manage some aspect of the application by calling it from a external management client.

+ *

This annotation can't be used together with {@link ManagedProperty}, doing so will throw a {@link DemoiselleException}.

+ * + * @author SERPRO + * + */ +@Documented +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ManagedOperation { + + /** + * Description that will be used to publish the operation to clients. + * Defaults to an empty description. + */ + @Nonbinding + String description() default ""; + + /** + * Type of operation. Defaults to {@link OperationType#UNKNOWN}. + */ + @Nonbinding + OperationType type() default OperationType.UNKNOWN; + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedProperty.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedProperty.java new file mode 100644 index 0000000..8236b63 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/ManagedProperty.java @@ -0,0 +1,69 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.enterprise.util.Nonbinding; + +/** + *

Indicates that a field must be exposed as a property to management clients.

+ *

The property will be writable if there's a public setter method + * declared for the field and readable if there's a getter method.

+ *

It's a runtime error to annotate a field with no getter and no setter method.

+ *

It's also a runtime error to declare a field as a property and one or both of it's getter and setter + * methods as an operation using the {@link ManagedOperation} annotation.

+ * + * @author SERPRO + * + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ManagedProperty { + + /** + * @return The description of this property exposed to management clients. + */ + @Nonbinding + String description() default ""; + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationParameter.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationParameter.java new file mode 100644 index 0000000..c010568 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationParameter.java @@ -0,0 +1,73 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.enterprise.util.Nonbinding; + +/** + *

Optional annotation to write additional detail about an operation's parameter.

+ *

This annotation is ignored for non-operation methods.

+ * + * @author SERPRO + * + */ +@Documented +@Target({ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +public @interface OperationParameter { + + /** + * Name that will be used to publish this operation's parameter to clients. + */ + @Nonbinding + String name(); + + /** + * Optional description that will be used to publish this operation's parameter to clients. + * Defaults to an empty description. + */ + @Nonbinding + String description() default ""; + + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationType.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationType.java new file mode 100644 index 0000000..2df9ea4 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/OperationType.java @@ -0,0 +1,89 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.annotation; + +import javax.management.MBeanOperationInfo; + + +/** + *

Define the operation type for an operation inside a ManagementController class.

+ * + *

This is an optional annotation and it's significanse will change based on the management extension + * used. Most extensions will just publish this information to the client so it can better show to the user the inner + * workings of the annotated operation.

+ * + * + * @author SERPRO + * + */ +public enum OperationType { + + /** + * ManagedOperation is write-only, it causes the application + * to change some of it's behaviour but doesn't return any kind of information + */ + ACTION(MBeanOperationInfo.ACTION) + , + /** + * ManagedOperation is read-only, it will operate over data provided by the application and return some information, + * but will not change the application in any way. + */ + INFO(MBeanOperationInfo.INFO) + , + /** + * ManagedOperation is read-write, it will both change the way the application work and return some information regarding + * the result of the operation. + */ + ACTION_INFO(MBeanOperationInfo.ACTION_INFO) + , + /** + * The effect of calling this operation is unknown. This is the default type and if this type is assigned to an operation, + * the user must rely on the {@link ManagedOperation#description()} attribute to learn about how the operation works. + */ + UNKNOWN(MBeanOperationInfo.UNKNOWN); + + private int operationTypeValue; + + private OperationType(int type){ + this.operationTypeValue = type; + } + + public int getValue(){ + return operationTypeValue; + } + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ManagementBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ManagementBootstrap.java index 23ef787..f9f81bd 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ManagementBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ManagementBootstrap.java @@ -17,10 +17,10 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType; import br.gov.frameworkdemoiselle.internal.context.ContextManager; import br.gov.frameworkdemoiselle.internal.context.ManagedContext; -import br.gov.frameworkdemoiselle.management.annotation.Managed; -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; -import br.gov.frameworkdemoiselle.management.internal.ManagedType; -import br.gov.frameworkdemoiselle.management.internal.MonitoringManager; +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.internal.management.Management; +import br.gov.frameworkdemoiselle.lifecycle.ManagementExtension; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; import br.gov.frameworkdemoiselle.util.Beans; public class ManagementBootstrap implements Extension { @@ -31,7 +31,7 @@ public class ManagementBootstrap implements Extension { public void detectAnnotation(@Observes final ProcessAnnotatedType event, final BeanManager beanManager) { - if (event.getAnnotatedType().isAnnotationPresent(Managed.class)) { + if (event.getAnnotatedType().isAnnotationPresent(ManagementController.class)) { types.add(event.getAnnotatedType()); } } @@ -43,7 +43,7 @@ public class ManagementBootstrap implements Extension { @SuppressWarnings("unchecked") public void registerAvailableManagedTypes(@Observes final AfterDeploymentValidation event,BeanManager beanManager) { - MonitoringManager monitoringManager = Beans.getReference(MonitoringManager.class); + Management monitoringManager = Beans.getReference(Management.class); for (AnnotatedType type : types) { ManagedType managedType = new ManagedType(type.getJavaClass()); monitoringManager.addManagedType(managedType); @@ -62,7 +62,7 @@ public class ManagementBootstrap implements Extension { public void unregisterAvailableManagedTypes(@Observes final BeforeShutdown event) { - MonitoringManager manager = Beans.getReference(MonitoringManager.class); + Management manager = Beans.getReference(Management.class); manager.shutdown(managementExtensionCache); managementExtensionCache.clear(); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ManagedContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ManagedContext.java index 00873cc..e07590c 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ManagedContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ManagedContext.java @@ -2,10 +2,10 @@ package br.gov.frameworkdemoiselle.internal.context; import javax.enterprise.context.RequestScoped; -import br.gov.frameworkdemoiselle.management.annotation.Managed; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; /** - * Context that stores {@link RequestScoped} beans during client calls to {@link Managed} classes. + * Context that stores {@link RequestScoped} beans during client calls to {@link ManagementController} classes. * This context is only activated when no other context is active for {@link RequestScoped}. * * @author serpro diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagedType.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagedType.java new file mode 100644 index 0000000..57b1051 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagedType.java @@ -0,0 +1,334 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.management; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.TreeMap; + +import br.gov.frameworkdemoiselle.DemoiselleException; +import br.gov.frameworkdemoiselle.annotation.ManagedOperation; +import br.gov.frameworkdemoiselle.annotation.ManagedProperty; +import br.gov.frameworkdemoiselle.annotation.OperationParameter; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; +import br.gov.frameworkdemoiselle.util.ResourceBundle; + +/** + *

Package class containing information about a discovered {@link ManagementController}.

+ * + *

Instances if this class are passed to each discovered management extension during bootstrap so they can have + * enough information to expose all discovered {@link ManagementController}'s to management clients.

+ * + * @author serpro + */ +public class ManagedType { + + private Class type; + + private TreeMap fields; + + private TreeMap operationMethods; + + private ResourceBundle bundle; + + private String description; + + public ManagedType(Class type) { + bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); + + if (type == null) { + throw new DemoiselleException(bundle.getString("management-null-class-defined")); + } + if (!type.isAnnotationPresent(ManagementController.class)) { + throw new DemoiselleException(bundle.getString("management-no-annotation-found", type.getCanonicalName())); + } + + this.type = type; + fields = new TreeMap(); + operationMethods = new TreeMap(); + this.description = type.getAnnotation(ManagementController.class).description(); + + initialize(); + } + + public Class getType() { + return type; + } + + public String getDescription() { + return description; + } + + public TreeMap getFields() { + return fields; + } + + public TreeMap getOperationMethods() { + return operationMethods; + } + + private void initialize() { + // Para cada atributo verifica se ele está anotado com ManagedProperty e extrai as informações dele (método get, set e + // descrição do atributo). + Field[] fields = type.getDeclaredFields(); + if (fields != null) { + for (Field field : fields) { + if (field.isAnnotationPresent(ManagedProperty.class)) { + // Obtém os métodos GET e SET para esta propriedade + Method getterMethod = getGetterMethod(field); + Method setterMethod = getSetterMethod(field); + if (getterMethod == null && setterMethod == null) { + throw new DemoiselleException(bundle.getString("management-invalid-property-no-getter-setter", + type.getSimpleName(), field.getName())); + } else if ((getterMethod != null && getterMethod.isAnnotationPresent(ManagedOperation.class)) + || (setterMethod != null && setterMethod.isAnnotationPresent(ManagedOperation.class))) { + throw new DemoiselleException(bundle.getString("management-invalid-property-as-operation", + type.getSimpleName())); + } + + String propertyDescription = field.getAnnotation(ManagedProperty.class).description(); + + this.fields.put(field.getName(), new FieldDetail(field, propertyDescription, getterMethod, + setterMethod)); + } + } + } + + // Para cada metodo verifica se ele está anotado com ManagedOperation e cria um MBeanOperationInfo para ele. + Method[] methodList = type.getMethods(); + if (methodList != null) { + for (Method method : methodList) { + ManagedOperation opAnnotation = method.getAnnotation(ManagedOperation.class); + + if (opAnnotation != null) { + // Lemos as informações sobre o método e criamos uma instância + // de MethodDetail para representar este método como uma + // operação. + + Class[] parameterTypes = method.getParameterTypes(); + Annotation[][] parameterAnnotations = method.getParameterAnnotations(); + ParameterDetail[] parameterDetails = new ParameterDetail[parameterTypes.length]; + + for (int i = 0; i < parameterTypes.length; i++) { + OperationParameter paramAnnotation = null; + for (Annotation annotation : parameterAnnotations[i]) { + if (annotation.annotationType() == OperationParameter.class) { + paramAnnotation = (OperationParameter) annotation; + break; + } + } + + String name = paramAnnotation != null ? paramAnnotation.name() : ("arg" + i); + String description = paramAnnotation != null ? paramAnnotation.description() : null; + + parameterDetails[i] = new ParameterDetail(parameterTypes[i], name, description); + } + + // Com todas as informações, criamos nossa instância de MethodDetail e + // acrescentamos na lista de todas as operações. + MethodDetail detail = new MethodDetail(method, opAnnotation.description(), parameterDetails); + operationMethods.put(method.getName(), detail); + } + } + } + } + + /** + * Returns the public getter method for a given field, or null if no getter method can be found. + */ + private Method getGetterMethod(Field field) { + StringBuffer getterMethodName = new StringBuffer() + .append("get") + .append(field.getName().substring(0, 1).toUpperCase()) + .append(field.getName().substring(1)); + + Method getterMethod; + + try { + getterMethod = type.getMethod(getterMethodName.toString()); + } catch (Exception e) { + getterMethod = null; + } + + // Se atributo for boolean, procura método getter no formato "isAttribute". + if (getterMethod == null + && (Boolean.TYPE.isAssignableFrom(field.getType()) || Boolean.class.isAssignableFrom(field.getType()))) { + // Boolean.TYPE representa o tipo primitivo "boolean", Boolean.class é a classe wrapper. + getterMethodName = new StringBuffer() + .append("is") + .append(field.getName().substring(0, 1).toUpperCase()) + .append(field.getName().substring(1).toUpperCase()); + + try { + getterMethod = type.getMethod(getterMethodName.toString()); + } catch (Exception e) { + getterMethod = null; + } + } + + return getterMethod; + } + + /** + * Returns the public setter method for a given field, or null if no setter method can be found. + */ + private Method getSetterMethod(Field field) { + StringBuffer setterMethodName = new StringBuffer() + .append("set") + .append(field.getName().substring(0, 1).toUpperCase()) + .append(field.getName().substring(1)); + + Method setterMethod; + + try { + setterMethod = type.getMethod(setterMethodName.toString() , field.getType()); + } catch (Exception e) { + setterMethod = null; + } + + return setterMethod; + } + + public final class FieldDetail { + + private final Field field; + + private final String description; + + private Method getterMethod; + + private Method setterMethod; + + public FieldDetail(Field field, String description, Method getterMethod, Method setterMethod) { + super(); + this.field = field; + this.description = description; + this.getterMethod = getterMethod; + this.setterMethod = setterMethod; + } + + public Field getField() { + return field; + } + + public String getDescription() { + return description; + } + + public Method getGetterMethod() { + return getterMethod; + } + + public Method getSetterMethod() { + return setterMethod; + } + + } + + public final class MethodDetail { + + private final Method method; + + private final ParameterDetail[] parameterTypers; + + private String description; + + public MethodDetail(Method method, String description, ParameterDetail[] parameterTypers) { + super(); + this.method = method; + this.description = description; + this.parameterTypers = parameterTypers; + } + + public Method getMethod() { + return method; + } + + public ParameterDetail[] getParameterTypers() { + return parameterTypers; + } + + public String getDescription() { + return description; + } + + } + + public final class ParameterDetail { + + private final Class parameterType; + + private final String parameterName; + + private final String parameterDescription; + + public ParameterDetail(Class parameterType, String parameterName, String parameterDescription) { + super(); + this.parameterType = parameterType; + this.parameterName = parameterName; + this.parameterDescription = parameterDescription; + } + + public Class getParameterType() { + return parameterType; + } + + public String getParameterName() { + return parameterName; + } + + public String getParameterDescription() { + return parameterDescription; + } + } + + /** + * Indicates another {@link ManagedType} represents the same {@link Class} as this one. This method also supports a + * {@link Class} as a parameter, in this case it will return true if the passed class is exactly the + * same Java class represented by this {@link ManagedType}. + */ + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } + + return ((ManagedType) other).getType().getCanonicalName().equals(this.getType().getCanonicalName()); + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/Management.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/Management.java new file mode 100644 index 0000000..2f3133b --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/Management.java @@ -0,0 +1,310 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.management; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.management.ReflectionException; + +import org.slf4j.Logger; + +import br.gov.frameworkdemoiselle.DemoiselleException; +import br.gov.frameworkdemoiselle.annotation.ManagedProperty; +import br.gov.frameworkdemoiselle.annotation.Name; +import br.gov.frameworkdemoiselle.internal.context.ContextManager; +import br.gov.frameworkdemoiselle.internal.context.ManagedContext; +import br.gov.frameworkdemoiselle.internal.management.ManagedType.MethodDetail; +import br.gov.frameworkdemoiselle.lifecycle.ManagementExtension; +import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; +import br.gov.frameworkdemoiselle.management.NotificationManager; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; +import br.gov.frameworkdemoiselle.util.Beans; +import br.gov.frameworkdemoiselle.util.ResourceBundle; + +/** + * Central class used by management extensions to obtain information, access properties and call operations + * over discovered {@link ManagementController} classes. + * + * @author serpro + */ +@ApplicationScoped +public class Management { + + @Inject + private Logger logger; + + @Inject + @Name("demoiselle-core-bundle") + private ResourceBundle bundle; + + private final List managedTypes = new ArrayList(); + + public void addManagedType(ManagedType managedType) { + managedTypes.add(managedType); + logger.debug(bundle.getString("management-debug-registering-managed-type",managedType.getType().getCanonicalName())); + } + + /** + * @return List all discovered {@link ManagementController} classes. + * The returned list is a shallow copy of the internal list, so you + * are free to modify it. + * + * TODO precisamos desse clone na lista? + */ + public List getManagedTypes() { + ArrayList cloneList = new ArrayList(); + cloneList.addAll(managedTypes); + return cloneList; + } + + /** + *

Invoke an operation over a {@link ManagementController}.

+ * + *

This method is not thread-safe, it's the user's responsibility to + * make the operations of the managed type synchronized if necessary.

+ * + * @param managedType + * A type annotated with {@link ManagementController}. This method will create + * an (or obtain an already created) instance of this type and + * invoke the operation over it. + * @param actionName + * Name of method to be invoked, the type must have this + * operation on it's list + * @param params + * List of values for the operation parameters. Can be + * null if the operation require no parameters. + * @return The return value of the original invoked operation. Methods of + * return type void will return the {@link Void} type. + * @throws ReflectionException + * In case the operation doesn't exist or have a different + * signature + */ + public Object invoke(ManagedType managedType, String actionName, + Object[] params) { + if ( managedTypes.contains(managedType) ) { + activateContexts(managedType.getType()); + + Object delegate = Beans.getReference(managedType.getType()); + + MethodDetail method = managedType.getOperationMethods().get(actionName); + + if (method != null) { + try { + logger.debug(bundle + .getString("management-debug-invoking-operation",actionName,managedType.getType().getCanonicalName())); + return method.getMethod().invoke(delegate, params); + } catch (Exception e) { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", actionName), e); + } finally { + deactivateContexts(managedType.getType()); + } + } else { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", actionName)); + } + } else { + throw new DemoiselleException( + bundle.getString("management-type-not-found")); + } + } + + /** + *

Retrieve the current value of a property from a managed type. Properties + * are attributes annotated with {@link ManagedProperty}.

+ * + *

This method is not thread-safe, it's the user's responsibility to + * create the property's access methods from the managed type synchronized if necessary.

+ * + * @param managedType The type that has the property the client wants to know the value of. + * @param propertyName The name of the property + * @return The current value of the property + */ + public Object getProperty(ManagedType managedType, String propertyName) { + + if ( managedTypes.contains(managedType) ) { + Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod(); + + if (getterMethod != null) { + logger.debug(bundle.getString( + "management-debug-acessing-property", getterMethod + .getName(), managedType.getType().getCanonicalName())); + + activateContexts(managedType.getType()); + + try { + Object delegate = Beans.getReference(managedType.getType()); + + return getterMethod.invoke(delegate, (Object[]) null); + } catch (Exception e) { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", getterMethod.getName()), + e); + } finally { + deactivateContexts(managedType.getType()); + } + } else { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", propertyName)); + } + } else { + throw new DemoiselleException( + bundle.getString("management-type-not-found")); + } + } + + /** + *

Sets a new value for a property contained inside a managed type. A property + * is an attribute annotated with {@link ManagedProperty}.

+ * + *

This method is not thread-safe, it's the user's responsibility to + * create the property's access methods from the managed type synchronized if necessary.

+ * + * @param managedType The type that has access to the property + * @param propertyName The name of the property + * @param newValue The new value of the property + */ + public void setProperty(ManagedType managedType, String propertyName, + Object newValue) { + + if ( managedTypes.contains(managedType) ) { + // Procura o método set do atributo em questão + Method method = managedType.getFields().get(propertyName).getSetterMethod(); + if (method != null) { + logger.debug(bundle.getString( + "management-debug-setting-property", method.getName(), + managedType.getType().getCanonicalName())); + + // Obtém uma instância da classe gerenciada, lembrando que + // classes + // anotadas com @ManagementController são sempre singletons. + activateContexts(managedType.getType()); + try { + Object delegate = Beans.getReference(managedType.getType()); + + Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod(); + Object oldValue; + try{ + oldValue = getterMethod.invoke(delegate, (Object[])null); + } + catch(Exception e){ + oldValue = null; + } + + method.invoke(delegate, new Object[] { newValue }); + + //Manda uma notificação de mudança de atributo + NotificationManager notificationManager = Beans.getReference(NotificationManager.class); + Class attributeType = newValue!=null ? newValue.getClass() : null; + + AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString("management-notification-attribute-changed",propertyName,managedType.getType().getCanonicalName()) + , propertyName + , attributeType + , oldValue + , newValue); + notificationManager.sendNotification(notification); + + } catch (Exception e) { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", method.getName()), e); + } finally { + deactivateContexts(managedType.getType()); + } + + } else { + throw new DemoiselleException(bundle.getString( + "management-invoke-error", propertyName)); + } + } else { + throw new DemoiselleException( + bundle.getString("management-type-not-found")); + } + + } + + private void activateContexts(Class managedType) { + logger.debug(bundle.getString("management-debug-starting-custom-context", + ManagedContext.class.getCanonicalName(), + managedType.getCanonicalName())); + + ContextManager.activate(ManagedContext.class,RequestScoped.class); + } + + private void deactivateContexts(Class managedType) { + logger.debug(bundle.getString("management-debug-stoping-custom-context", + ManagedContext.class.getCanonicalName(), + managedType.getCanonicalName())); + + ContextManager.deactivate(ManagedContext.class,RequestScoped.class); + } + + public void shutdown(Collection> monitoringExtensions) { + + for (Class monitoringExtensionClass : monitoringExtensions) { + + ManagementExtension monitoringExtension = Beans.getReference(monitoringExtensionClass); + + monitoringExtension.shutdown(this.getManagedTypes()); + + logger.debug( bundle.getString("management-debug-removing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); + + } + + } + + public void initialize(Collection> monitoringExtensions) { + + for (Class monitoringExtensionClass : monitoringExtensions) { + + ManagementExtension monitoringExtension = Beans + .getReference(monitoringExtensionClass); + + monitoringExtension.initialize(this.getManagedTypes()); + + logger.debug( bundle.getString("management-debug-processing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); + + } + + } + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagementNotificationEvent.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagementNotificationEvent.java new file mode 100644 index 0000000..3beea45 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/ManagementNotificationEvent.java @@ -0,0 +1,65 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.management; + +import br.gov.frameworkdemoiselle.management.Notification; +import br.gov.frameworkdemoiselle.management.NotificationManager; + +/** + * Event fired when a notification is sent by {@link NotificationManager}. + * Implementators can capture this event and be notified when the {@link NotificationManager} + * sends notifications, so they can pass the notification to the underlying technology. + * + * @author serpro + * + */ +public class ManagementNotificationEvent { + + private Notification notification; + + public ManagementNotificationEvent(Notification notification){ + this.notification = notification; + } + + public Notification getNotification() { + return notification; + } + + public void setNotification(Notification notification) { + this.notification = notification; + } +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/AttributeChange.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/AttributeChange.java new file mode 100644 index 0000000..8889cd2 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/AttributeChange.java @@ -0,0 +1,62 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.management.qualifier; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +import br.gov.frameworkdemoiselle.internal.management.ManagementNotificationEvent; +import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; + +/** + * + * Enables {@link ManagementNotificationEvent} observers to trigger only with notifications + * of the specialized type {@link AttributeChangeNotification}. + * + * @author serpro + * + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) +public @interface AttributeChange { + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/Generic.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/Generic.java new file mode 100644 index 0000000..1c75fb0 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/management/qualifier/Generic.java @@ -0,0 +1,62 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.management.qualifier; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +import br.gov.frameworkdemoiselle.internal.management.ManagementNotificationEvent; +import br.gov.frameworkdemoiselle.management.Notification; + +/** + * + * Enables {@link ManagementNotificationEvent} observers to trigger only with notifications + * of the base type {@link Notification}. + * + * @author serpro + * + */ +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) +public @interface Generic { + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/validation/AllowedValuesValidator.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/validation/AllowedValuesValidator.java new file mode 100644 index 0000000..23d5e2b --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/validation/AllowedValuesValidator.java @@ -0,0 +1,104 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.validation; + +import java.math.BigDecimal; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +import br.gov.frameworkdemoiselle.validation.AllowedValues; + + +public class AllowedValuesValidator implements ConstraintValidator { + + private br.gov.frameworkdemoiselle.validation.AllowedValues.ValueType valueType; + private String[] allowedValues; + + @Override + public void initialize(AllowedValues constraintAnnotation) { + valueType = constraintAnnotation.valueType(); + allowedValues = constraintAnnotation.allows(); + } + + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { + + if (value==null){ + return false; + } + + switch(valueType){ + case STRING: + for (String str : allowedValues){ + if (str.equals(value)) return true; + } + return false; + + case INTEGER: + try{ + Integer number = Integer.valueOf(value.toString()); + String strNumber = number.toString(); + for (String str : allowedValues){ + if (str.equals(strNumber)) return true; + } + + return false; + } + catch(NumberFormatException ne){ + return false; + } + + case DECIMAL: + try{ + BigDecimal number = new BigDecimal(value.toString()); + String strNumber = number.toString(); + for (String str : allowedValues){ + if (str.equals(strNumber)) return true; + } + } + catch(NumberFormatException ne){ + return false; + } + + return false; + } + + return false; + } + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/lifecycle/ManagementExtension.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/lifecycle/ManagementExtension.java new file mode 100644 index 0000000..ed8abf7 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/lifecycle/ManagementExtension.java @@ -0,0 +1,76 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.lifecycle; + +import java.util.List; + +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; + +/** + * + *

Interface defining the lifecycle of a management extension, an extension + * capable of exposing {@link ManagementController}'s to external clients in one + * of the available management technologies, such as JMX or SNMP.

+ * + *

To include a management extension into the management lifecycle, it just needs + * to implement this interface and be a CDI bean (have a beans.xml file inside + * the META-INF folder of it's java package). The Demoiselle Core lifecycle controller + * will call the {@link #initialize(List managedTypes)} and {@link #shutdown(List managedTypes)} methods at the apropriate times.

+ * + * @author serpro + * + */ +public interface ManagementExtension { + + /** + * This method is called during the application initialization process for each concrete + * implementation of this interface. + * + * @param managedTypes The list of discovered {@link ManagementController} classes. + */ + void initialize(List managedTypes); + + /** + * This method is called during the application shutdown process for each concrete + * implementation of this interface. + * + * @param managedTypes The list of discovered {@link ManagementController} classes. + */ + void shutdown(List managedTypes); + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/AttributeChangeNotification.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/AttributeChangeNotification.java new file mode 100644 index 0000000..6a93f89 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/AttributeChangeNotification.java @@ -0,0 +1,110 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.management; + +/** + * Special notification to denote an attribute has changed values. + * + * @see Notification + * + * @author serpro + * + */ +public class AttributeChangeNotification extends Notification { + + private String attributeName; + + private Class attributeType; + + private Object oldValue; + + private Object newValue; + + public AttributeChangeNotification(){} + + public AttributeChangeNotification(Object message, String attributeName, Class attributeType, Object oldValue, + Object newValue) { + super(message); + this.attributeName = attributeName; + this.attributeType = attributeType; + this.oldValue = oldValue; + this.newValue = newValue; + } + + + public String getAttributeName() { + return attributeName; + } + + + public void setAttributeName(String attributeName) { + this.attributeName = attributeName; + } + + + public Class getAttributeType() { + return attributeType; + } + + + public void setAttributeType(Class attributeType) { + this.attributeType = attributeType; + } + + + public Object getOldValue() { + return oldValue; + } + + + public void setOldValue(Object oldValue) { + this.oldValue = oldValue; + } + + + public Object getNewValue() { + return newValue; + } + + + public void setNewValue(Object newValue) { + this.newValue = newValue; + } + + + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/Notification.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/Notification.java new file mode 100644 index 0000000..07d2c30 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/Notification.java @@ -0,0 +1,77 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.management; + +/** + * + * Notification that can be sent by the {@link NotificationManager}. + * + * @author serpro + * + */ +public class Notification { + + private Object message; + + public Notification(){ + } + + public Notification(Object message) { + super(); + this.message = message; + } + + + public Object getMessage() { + return message; + } + + + public void setMessage(Object message) { + this.message = message; + } + + + public Class getType() { + if (message!=null){ + return message.getClass(); + } + + return null; + } + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/NotificationManager.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/NotificationManager.java new file mode 100644 index 0000000..95f3f39 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/NotificationManager.java @@ -0,0 +1,116 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.management; + +import java.io.Serializable; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.util.AnnotationLiteral; +import javax.inject.Inject; + +import br.gov.frameworkdemoiselle.internal.management.ManagementNotificationEvent; +import br.gov.frameworkdemoiselle.internal.management.qualifier.AttributeChange; +import br.gov.frameworkdemoiselle.internal.management.qualifier.Generic; +import br.gov.frameworkdemoiselle.util.Beans; + +/** + * + *

Central class to manage sending notifications to management clients. + * This class allows applications to send management notifications without + * knowledge of the technology used to send those notifications.

+ * + *

To obtain an instance of the {@link NotificationManager} simply inject it in + * your code using {@link Inject} or the {@link Beans#getReference(Class beanType)} method. The {@link NotificationManager} + * is {@link ApplicationScoped}, so you can inject it as many times as needed and still have only one instance per application.

+ * + *

Implementators of management protocols must observe the {@link ManagementNotificationEvent} event (using the {@link Observes} annotation), this way + * they will receive an event containing the original notification and can translate this notification to a specific protocol. Optionaly, + * the implementator can use qualifiers like the {@link Generic} and {@link AttributeChange} qualifiers + * to filter what king of notifications they will handle. One example of an implementator is the demoiselle-jmx extension.

+ * + * @author serpro + * + */ +@ApplicationScoped +@SuppressWarnings("serial") +public class NotificationManager implements Serializable{ + + @Inject + @Generic + private Event genericNotificationEvent; + + @Inject + @AttributeChange + private Event attributeChangeNotificationEvent; + + /** + * Sends a generic notification to all management clients. + * + * @param notification The notification to send + */ + public void sendNotification(Notification notification) { + if (! AttributeChangeNotification.class.isInstance(notification) ){ + getGenericNotificationEvent().fire(new ManagementNotificationEvent(notification)); + } + else{ + getAttributeChangeNotificationEvent().fire(new ManagementNotificationEvent(notification)); + } + } + + @SuppressWarnings("unchecked") + private Event getGenericNotificationEvent() { + if (genericNotificationEvent==null){ + genericNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral() {}); + } + + return genericNotificationEvent; + } + + @SuppressWarnings("unchecked") + private Event getAttributeChangeNotificationEvent() { + if (attributeChangeNotificationEvent==null){ + attributeChangeNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral() {}); + } + + return attributeChangeNotificationEvent; + } + + + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Managed.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Managed.java deleted file mode 100644 index 7af7cbb..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Managed.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2011 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Stereotype; -import javax.enterprise.util.Nonbinding; -import javax.interceptor.InterceptorBinding; - -/** - * Stereotype to indicate a given class is managed. What it means is that an external client can manage the application - * this class is in by reading or writing it's attributes and calling it's operations. - *

- * Only fields annotated with {@link br.gov.frameworkdemoiselle.management.annotation.Property} or - * methods annotated with {@link br.gov.frameworkdemoiselle.management.annotation.Operation} will be exposed - * to clients.

- *

This stereotype only defines a class as managed, you need to choose an extension that will expose this managed class - * to external clients using any technology available. One example is the Demoiselle JMX extension, that will expose - * managed classes as MBeans.

- * - * @author SERPRO - */ -@Documented -@Stereotype -@InterceptorBinding -@Inherited -@Retention(RUNTIME) -@Target({ TYPE }) -@ApplicationScoped -public @interface Managed { - - /** - * @return Human readable description of this managed class. - */ - @Nonbinding - String description() default ""; - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Operation.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Operation.java deleted file mode 100644 index a7a5218..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Operation.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.enterprise.util.Nonbinding; -import javax.management.MBeanException; - -/** - *

Indicates that a method is an operation, meaning you can manage some aspect of the application by calling it.

- *

This annotation can't be used together with {@link Property}, doing so will throw a {@link MBeanException}.

- * - * @author SERPRO - * - */ -@Documented -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Operation { - - /** - * Description that will be used to publish the operation to clients. - * Defaults to an empty description. - */ - @Nonbinding - String description() default ""; - - /** - * Type of operation. Defaults to {@link OperationType#UNKNOWN}. - */ - @Nonbinding - OperationType type() default OperationType.UNKNOWN; - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationParameter.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationParameter.java deleted file mode 100644 index 0943576..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationParameter.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.enterprise.util.Nonbinding; - -/** - *

Optional annotation to write additional detail about an operation's parameter.

- *

This annotation is ignored for non-operation methods.

- * - * @author SERPRO - * - */ -@Documented -@Target({ElementType.PARAMETER}) -@Retention(RetentionPolicy.RUNTIME) -public @interface OperationParameter { - - /** - * Name that will be used to publish this operation's parameter to clients. - */ - @Nonbinding - String name(); - - /** - * Optional description that will be used to publish this operation's parameter to clients. - * Defaults to an empty description. - */ - @Nonbinding - String description() default ""; - - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationType.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationType.java deleted file mode 100644 index 37c1567..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/OperationType.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation; - -import javax.management.MBeanOperationInfo; - - -/** - * Define the operation type for an operation inside a Managed class. - * - * - * @author SERPRO - * - */ -public enum OperationType { - - /** - * Operation is write-only - */ - ACTION(MBeanOperationInfo.ACTION) - , - /** - * Operation is read-only - */ - INFO(MBeanOperationInfo.INFO) - , - /** - * Operation is read-write - */ - ACTION_INFO(MBeanOperationInfo.ACTION_INFO) - , - /** - * Operation is unkown - */ - UNKNOWN(MBeanOperationInfo.UNKNOWN); - - private int operationTypeValue; - - private OperationType(int type){ - this.operationTypeValue = type; - } - - public int getValue(){ - return operationTypeValue; - } - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Property.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Property.java deleted file mode 100644 index 61720d5..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/Property.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.enterprise.util.Nonbinding; - -/** - *

Indicates that a field must be exposed as a property to management clients.

- *

The property will be writable if there's a public setter method - * declared for the field and readable if there's a getter method.

- *

It's a runtime error to annotate a field with no getter and no setter method.

- *

It's also a runtime error to declare a field as a property and one or both of it's getter and setter - * methods as an operation using the {@link Operation} annotation.

- * - * @author SERPRO - * - */ -@Documented -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface Property { - - /** - * @return The description of this property exposed to management clients. - */ - @Nonbinding - String description() default ""; - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/validation/AllowedValues.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/validation/AllowedValues.java deleted file mode 100644 index be0dee0..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/annotation/validation/AllowedValues.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.annotation.validation; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import javax.validation.Constraint; - -import br.gov.frameworkdemoiselle.management.internal.validators.AllowedValuesValidator; - -@Documented -@Target({ FIELD}) -@Retention(RUNTIME) -@Constraint(validatedBy = AllowedValuesValidator.class) -/** - * Validate a value against a list of allowed values. - * - * @author serpro - * - */ -public @interface AllowedValues { - - /** - * List of accepted values - */ - String[] allows(); - - /** - * Type of allowed values. Defaults to {@link ValueType#STRING}. - */ - ValueType valueType() default ValueType.STRING; - - enum ValueType { - STRING,INTEGER,DECIMAL; - } - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/extension/ManagementExtension.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/extension/ManagementExtension.java deleted file mode 100644 index 564f3d6..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/extension/ManagementExtension.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.extension; - -import java.util.List; - -import br.gov.frameworkdemoiselle.management.internal.ManagedType; - -/** - * - * Define an entry point for monitoring extension. - * - * @author serpro - * - */ -public interface ManagementExtension { - - void initialize(List managedTypes); - - void shutdown(List managedTypes); - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/ManagedType.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/ManagedType.java deleted file mode 100644 index 9789e4a..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/ManagedType.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.TreeMap; - -import br.gov.frameworkdemoiselle.DemoiselleException; -import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; -import br.gov.frameworkdemoiselle.management.annotation.Managed; -import br.gov.frameworkdemoiselle.management.annotation.Operation; -import br.gov.frameworkdemoiselle.management.annotation.OperationParameter; -import br.gov.frameworkdemoiselle.management.annotation.Property; -import br.gov.frameworkdemoiselle.util.ResourceBundle; - -/** - * Represents a detected managed type, a Java class annotated with {@link Managed}. - * - * @author serpro - */ -public class ManagedType { - - private Class type; - - private TreeMap fields; - - private TreeMap operationMethods; - - private ResourceBundle bundle; - - private String description; - - public ManagedType(Class type) { - bundle = ResourceBundleProducer.create("demoiselle-core-bundle"); - - if (type == null) { - throw new DemoiselleException(bundle.getString("management-null-class-defined")); - } - if (!type.isAnnotationPresent(Managed.class)) { - throw new DemoiselleException(bundle.getString("management-no-annotation-found", type.getCanonicalName())); - } - - this.type = type; - fields = new TreeMap(); - operationMethods = new TreeMap(); - this.description = type.getAnnotation(Managed.class).description(); - - initialize(); - } - - public Class getType() { - return type; - } - - public String getDescription() { - return description; - } - - public TreeMap getFields() { - return fields; - } - - public TreeMap getOperationMethods() { - return operationMethods; - } - - private void initialize() { - // Para cada atributo verifica se ele está anotado com Property e extrai as informações dele (método get, set e - // descrição do atributo). - Field[] fields = type.getDeclaredFields(); - if (fields != null) { - for (Field field : fields) { - if (field.isAnnotationPresent(Property.class)) { - // Obtém os métodos GET e SET para esta propriedade - Method getterMethod = getGetterMethod(field); - Method setterMethod = getSetterMethod(field); - if (getterMethod == null && setterMethod == null) { - throw new DemoiselleException(bundle.getString("management-invalid-property-no-getter-setter", - type.getSimpleName(), field.getName())); - } else if ((getterMethod != null && getterMethod.isAnnotationPresent(Operation.class)) - || (setterMethod != null && setterMethod.isAnnotationPresent(Operation.class))) { - throw new DemoiselleException(bundle.getString("management-invalid-property-as-operation", - type.getSimpleName())); - } - - String propertyDescription = field.getAnnotation(Property.class).description(); - - this.fields.put(field.getName(), new FieldDetail(field, propertyDescription, getterMethod, - setterMethod)); - } - } - } - - // Para cada metodo verifica se ele está anotado com Operation e cria um MBeanOperationInfo para ele. - Method[] methodList = type.getMethods(); - if (methodList != null) { - for (Method method : methodList) { - Operation opAnnotation = method.getAnnotation(Operation.class); - - if (opAnnotation != null) { - // Lemos as informações sobre o método e criamos uma instância - // de MethodDetail para representar este método como uma - // operação. - - Class[] parameterTypes = method.getParameterTypes(); - Annotation[][] parameterAnnotations = method.getParameterAnnotations(); - ParameterDetail[] parameterDetails = new ParameterDetail[parameterTypes.length]; - - for (int i = 0; i < parameterTypes.length; i++) { - OperationParameter paramAnnotation = null; - for (Annotation annotation : parameterAnnotations[i]) { - if (annotation.annotationType() == OperationParameter.class) { - paramAnnotation = (OperationParameter) annotation; - break; - } - } - - String name = paramAnnotation != null ? paramAnnotation.name() : ("arg" + i); - String description = paramAnnotation != null ? paramAnnotation.description() : null; - - parameterDetails[i] = new ParameterDetail(parameterTypes[i], name, description); - } - - // Com todas as informações, criamos nossa instância de MethodDetail e - // acrescentamos na lista de todas as operações. - MethodDetail detail = new MethodDetail(method, opAnnotation.description(), parameterDetails); - operationMethods.put(method.getName(), detail); - } - } - } - } - - /** - * Returns the public getter method for a given field, or null if no getter method can be found. - */ - private Method getGetterMethod(Field field) { - StringBuffer getterMethodName = new StringBuffer() - .append("get") - .append(field.getName().substring(0, 1).toUpperCase()) - .append(field.getName().substring(1)); - - Method getterMethod; - - try { - getterMethod = type.getMethod(getterMethodName.toString()); - } catch (Exception e) { - getterMethod = null; - } - - // Se atributo for boolean, procura método getter no formato "isAttribute". - if (getterMethod == null - && (Boolean.TYPE.isAssignableFrom(field.getType()) || Boolean.class.isAssignableFrom(field.getType()))) { - // Boolean.TYPE representa o tipo primitivo "boolean", Boolean.class é a classe wrapper. - getterMethodName = new StringBuffer() - .append("is") - .append(field.getName().substring(0, 1).toUpperCase()) - .append(field.getName().substring(1).toUpperCase()); - - try { - getterMethod = type.getMethod(getterMethodName.toString()); - } catch (Exception e) { - getterMethod = null; - } - } - - return getterMethod; - } - - /** - * Returns the public setter method for a given field, or null if no setter method can be found. - */ - private Method getSetterMethod(Field field) { - StringBuffer setterMethodName = new StringBuffer() - .append("set") - .append(field.getName().substring(0, 1).toUpperCase()) - .append(field.getName().substring(1)); - - Method setterMethod; - - try { - setterMethod = type.getMethod(setterMethodName.toString() , field.getType()); - } catch (Exception e) { - setterMethod = null; - } - - return setterMethod; - } - - public final class FieldDetail { - - private final Field field; - - private final String description; - - private Method getterMethod; - - private Method setterMethod; - - public FieldDetail(Field field, String description, Method getterMethod, Method setterMethod) { - super(); - this.field = field; - this.description = description; - this.getterMethod = getterMethod; - this.setterMethod = setterMethod; - } - - public Field getField() { - return field; - } - - public String getDescription() { - return description; - } - - public Method getGetterMethod() { - return getterMethod; - } - - public Method getSetterMethod() { - return setterMethod; - } - - } - - public final class MethodDetail { - - private final Method method; - - private final ParameterDetail[] parameterTypers; - - private String description; - - public MethodDetail(Method method, String description, ParameterDetail[] parameterTypers) { - super(); - this.method = method; - this.description = description; - this.parameterTypers = parameterTypers; - } - - public Method getMethod() { - return method; - } - - public ParameterDetail[] getParameterTypers() { - return parameterTypers; - } - - public String getDescription() { - return description; - } - - } - - public final class ParameterDetail { - - private final Class parameterType; - - private final String parameterName; - - private final String parameterDescription; - - public ParameterDetail(Class parameterType, String parameterName, String parameterDescription) { - super(); - this.parameterType = parameterType; - this.parameterName = parameterName; - this.parameterDescription = parameterDescription; - } - - public Class getParameterType() { - return parameterType; - } - - public String getParameterName() { - return parameterName; - } - - public String getParameterDescription() { - return parameterDescription; - } - } - - /** - * Indicates another {@link ManagedType} represents the same {@link Class} as this one. This method also supports a - * {@link Class} as a parameter, in this case it will return true if the passed class is exactly the - * same Java class represented by this {@link ManagedType}. - */ - @Override - public boolean equals(Object other) { - if (other == null) { - return false; - } - - return ((ManagedType) other).getType().getCanonicalName().equals(this.getType().getCanonicalName()); - } -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/MonitoringManager.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/MonitoringManager.java deleted file mode 100644 index 88fbcc0..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/MonitoringManager.java +++ /dev/null @@ -1,311 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; -import javax.management.ReflectionException; - -import org.slf4j.Logger; - -import br.gov.frameworkdemoiselle.DemoiselleException; -import br.gov.frameworkdemoiselle.annotation.Name; -import br.gov.frameworkdemoiselle.internal.context.ContextManager; -import br.gov.frameworkdemoiselle.internal.context.ManagedContext; -import br.gov.frameworkdemoiselle.management.annotation.Managed; -import br.gov.frameworkdemoiselle.management.annotation.Property; -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; -import br.gov.frameworkdemoiselle.management.internal.ManagedType.MethodDetail; -import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; -import br.gov.frameworkdemoiselle.management.notification.NotificationManager; -import br.gov.frameworkdemoiselle.util.Beans; -import br.gov.frameworkdemoiselle.util.ResourceBundle; - -/** - * A manager that helps implementators of the management framework to obtain a - * list of managed classes, set or obtain values from them or invoke operations - * over them. - * - * @author serpro - */ -@ApplicationScoped -public class MonitoringManager { - - @Inject - private Logger logger; - - @Inject - @Name("demoiselle-core-bundle") - private ResourceBundle bundle; - - private final List managedTypes = new ArrayList(); - - public void addManagedType(ManagedType managedType) { - managedTypes.add(managedType); - logger.debug(bundle.getString("management-debug-registering-managed-type",managedType.getType().getCanonicalName())); - } - - /** - * @return A list all managed types, classes annotated with {@link Managed}. - * The returned list is a shallow copy of the internal list, so you - * are free to modify it. - * - * TODO precisamos desse clone na lista? - */ - public List getManagedTypes() { - ArrayList cloneList = new ArrayList(); - cloneList.addAll(managedTypes); - return cloneList; - } - - /** - *

Invoke an operation over a managed type.

- * - *

This method is not thread-safe, it's the user's responsibility to - * make the operations of the managed type synchronized if necessary.

- * - * @param managedType - * A type annotated with {@link Managed}. This method will create - * an (or obtain an already created) instance of this type and - * invoke the operation over it. - * @param actionName - * Name of method to be invoked, the type must have this - * operation on it's list - * @param params - * List of values for the operation parameters. Can be - * null if the operation require no parameters. - * @return The return value of the original invoked operation. Methods of - * return type void will return the {@link Void} type. - * @throws ReflectionException - * In case the operation doesn't exist or have a different - * signature - */ - public Object invoke(ManagedType managedType, String actionName, - Object[] params) { - if ( managedTypes.contains(managedType) ) { - activateContexts(managedType.getType()); - - Object delegate = Beans.getReference(managedType.getType()); - - MethodDetail method = managedType.getOperationMethods().get(actionName); - - if (method != null) { - try { - logger.debug(bundle - .getString("management-debug-invoking-operation",actionName,managedType.getType().getCanonicalName())); - return method.getMethod().invoke(delegate, params); - } catch (Exception e) { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", actionName), e); - } finally { - deactivateContexts(managedType.getType()); - } - } else { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", actionName)); - } - } else { - throw new DemoiselleException( - bundle.getString("management-type-not-found")); - } - } - - /** - *

Retrieve the current value of a property from a managed type. Properties - * are attributes annotated with {@link Property}.

- * - *

This method is not thread-safe, it's the user's responsibility to - * create the property's access methods from the managed type synchronized if necessary.

- * - * @param managedType The type that has the property the client wants to know the value of. - * @param propertyName The name of the property - * @return The current value of the property - */ - public Object getProperty(ManagedType managedType, String propertyName) { - - if ( managedTypes.contains(managedType) ) { - Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod(); - - if (getterMethod != null) { - logger.debug(bundle.getString( - "management-debug-acessing-property", getterMethod - .getName(), managedType.getType().getCanonicalName())); - - activateContexts(managedType.getType()); - - try { - Object delegate = Beans.getReference(managedType.getType()); - - return getterMethod.invoke(delegate, (Object[]) null); - } catch (Exception e) { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", getterMethod.getName()), - e); - } finally { - deactivateContexts(managedType.getType()); - } - } else { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", propertyName)); - } - } else { - throw new DemoiselleException( - bundle.getString("management-type-not-found")); - } - } - - /** - *

Sets a new value for a property contained inside a managed type. A property - * is an attribute annotated with {@link Property}.

- * - *

This method is not thread-safe, it's the user's responsibility to - * create the property's access methods from the managed type synchronized if necessary.

- * - * @param managedType The type that has access to the property - * @param propertyName The name of the property - * @param newValue The new value of the property - */ - public void setProperty(ManagedType managedType, String propertyName, - Object newValue) { - - if ( managedTypes.contains(managedType) ) { - // Procura o método set do atributo em questão - Method method = managedType.getFields().get(propertyName).getSetterMethod(); - if (method != null) { - logger.debug(bundle.getString( - "management-debug-setting-property", method.getName(), - managedType.getType().getCanonicalName())); - - // Obtém uma instância da classe gerenciada, lembrando que - // classes - // anotadas com @Managed são sempre singletons. - activateContexts(managedType.getType()); - try { - Object delegate = Beans.getReference(managedType.getType()); - - Method getterMethod = managedType.getFields().get(propertyName).getGetterMethod(); - Object oldValue; - try{ - oldValue = getterMethod.invoke(delegate, (Object[])null); - } - catch(Exception e){ - oldValue = null; - } - - method.invoke(delegate, new Object[] { newValue }); - - //Manda uma notificação de mudança de atributo - NotificationManager notificationManager = Beans.getReference(NotificationManager.class); - Class attributeType = newValue!=null ? newValue.getClass() : null; - - AttributeChangeNotification notification = new AttributeChangeNotification(bundle.getString("management-notification-attribute-changed",propertyName,managedType.getType().getCanonicalName()) - , propertyName - , attributeType - , oldValue - , newValue); - notificationManager.sendNotification(notification); - - } catch (Exception e) { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", method.getName()), e); - } finally { - deactivateContexts(managedType.getType()); - } - - } else { - throw new DemoiselleException(bundle.getString( - "management-invoke-error", propertyName)); - } - } else { - throw new DemoiselleException( - bundle.getString("management-type-not-found")); - } - - } - - private void activateContexts(Class managedType) { - logger.debug(bundle.getString("management-debug-starting-custom-context", - ManagedContext.class.getCanonicalName(), - managedType.getCanonicalName())); - - ContextManager.activate(ManagedContext.class,RequestScoped.class); - } - - private void deactivateContexts(Class managedType) { - logger.debug(bundle.getString("management-debug-stoping-custom-context", - ManagedContext.class.getCanonicalName(), - managedType.getCanonicalName())); - - ContextManager.deactivate(ManagedContext.class,RequestScoped.class); - } - - public void shutdown(Collection> monitoringExtensions) { - - for (Class monitoringExtensionClass : monitoringExtensions) { - - ManagementExtension monitoringExtension = Beans.getReference(monitoringExtensionClass); - - monitoringExtension.shutdown(this.getManagedTypes()); - - logger.debug( bundle.getString("management-debug-removing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); - - } - - } - - public void initialize(Collection> monitoringExtensions) { - - for (Class monitoringExtensionClass : monitoringExtensions) { - - ManagementExtension monitoringExtension = Beans - .getReference(monitoringExtensionClass); - - monitoringExtension.initialize(this.getManagedTypes()); - - logger.debug( bundle.getString("management-debug-processing-management-extension",monitoringExtension.getClass().getCanonicalName()) ); - - } - - } - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/event/NotificationEvent.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/event/NotificationEvent.java deleted file mode 100644 index e63f401..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/event/NotificationEvent.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal.notification.event; - -import br.gov.frameworkdemoiselle.management.notification.Notification; -import br.gov.frameworkdemoiselle.management.notification.NotificationManager; - -/** - * Event fired when a notification is sent by {@link NotificationManager}. - * Implementators can capture this event and by notified when the {@link NotificationManager} - * sends notifications, so they can pass the notification to a underlying protocol such as JMX. - * - * @author serpro - * - */ -public class NotificationEvent { - - private Notification notification; - - public NotificationEvent(Notification notification){ - this.notification = notification; - } - - public Notification getNotification() { - return notification; - } - - public void setNotification(Notification notification) { - this.notification = notification; - } -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/AttributeChange.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/AttributeChange.java deleted file mode 100644 index aaf9ae1..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/AttributeChange.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal.notification.qualifier; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.inject.Qualifier; - -import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; -import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; - -/** - * - * Enables {@link NotificationEvent} observers to trigger only with notifications - * of the specialized type {@link AttributeChangeNotification}. - * - * @author serpro - * - */ -@Qualifier -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) -public @interface AttributeChange { - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/Generic.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/Generic.java deleted file mode 100644 index fa1f73e..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/notification/qualifier/Generic.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal.notification.qualifier; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.inject.Qualifier; - -import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; -import br.gov.frameworkdemoiselle.management.notification.Notification; - -/** - * - * Enables {@link NotificationEvent} observers to trigger only with notifications - * of the base type {@link Notification}. - * - * @author serpro - * - */ -@Qualifier -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) -public @interface Generic { - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/validators/AllowedValuesValidator.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/validators/AllowedValuesValidator.java deleted file mode 100644 index 6df5770..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/internal/validators/AllowedValuesValidator.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.internal.validators; - -import java.math.BigDecimal; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; - -import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues; - - -public class AllowedValuesValidator implements ConstraintValidator { - - private br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues.ValueType valueType; - private String[] allowedValues; - - @Override - public void initialize(AllowedValues constraintAnnotation) { - valueType = constraintAnnotation.valueType(); - allowedValues = constraintAnnotation.allows(); - } - - @Override - public boolean isValid(Object value, ConstraintValidatorContext context) { - - if (value==null){ - return false; - } - - switch(valueType){ - case STRING: - for (String str : allowedValues){ - if (str.equals(value)) return true; - } - return false; - - case INTEGER: - try{ - Integer number = Integer.valueOf(value.toString()); - String strNumber = number.toString(); - for (String str : allowedValues){ - if (str.equals(strNumber)) return true; - } - - return false; - } - catch(NumberFormatException ne){ - return false; - } - - case DECIMAL: - try{ - BigDecimal number = new BigDecimal(value.toString()); - String strNumber = number.toString(); - for (String str : allowedValues){ - if (str.equals(strNumber)) return true; - } - } - catch(NumberFormatException ne){ - return false; - } - - return false; - } - - return false; - } - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/AttributeChangeNotification.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/AttributeChangeNotification.java deleted file mode 100644 index 6fa1cea..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/AttributeChangeNotification.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.notification; - -/** - * Special notification to denote an attribute has changed values. - * - * @see Notification - * - * @author serpro - * - */ -public class AttributeChangeNotification extends Notification { - - private String attributeName; - - private Class attributeType; - - private Object oldValue; - - private Object newValue; - - public AttributeChangeNotification(){} - - public AttributeChangeNotification(Object message, String attributeName, Class attributeType, Object oldValue, - Object newValue) { - super(message); - this.attributeName = attributeName; - this.attributeType = attributeType; - this.oldValue = oldValue; - this.newValue = newValue; - } - - - public String getAttributeName() { - return attributeName; - } - - - public void setAttributeName(String attributeName) { - this.attributeName = attributeName; - } - - - public Class getAttributeType() { - return attributeType; - } - - - public void setAttributeType(Class attributeType) { - this.attributeType = attributeType; - } - - - public Object getOldValue() { - return oldValue; - } - - - public void setOldValue(Object oldValue) { - this.oldValue = oldValue; - } - - - public Object getNewValue() { - return newValue; - } - - - public void setNewValue(Object newValue) { - this.newValue = newValue; - } - - - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/Notification.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/Notification.java deleted file mode 100644 index da55daa..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/Notification.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.notification; - -/** - * - * Notification that can be sent by the {@link NotificationManager}. - * - * @author serpro - * - */ -public class Notification { - - private Object message; - - public Notification(){ - } - - public Notification(Object message) { - super(); - this.message = message; - } - - - public Object getMessage() { - return message; - } - - - public void setMessage(Object message) { - this.message = message; - } - - - public Class getType() { - if (message!=null){ - return message.getClass(); - } - - return null; - } - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/NotificationManager.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/NotificationManager.java deleted file mode 100644 index de38bda..0000000 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/management/notification/NotificationManager.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Demoiselle Framework - * Copyright (C) 2010 SERPRO - * ---------------------------------------------------------------------------- - * This file is part of Demoiselle Framework. - * - * Demoiselle Framework is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License version 3 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License version 3 - * along with this program; if not, see - * or write to the Free Software Foundation, Inc., 51 Franklin Street, - * Fifth Floor, Boston, MA 02110-1301, USA. - * ---------------------------------------------------------------------------- - * Este arquivo é parte do Framework Demoiselle. - * - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação - * do Software Livre (FSF). - * - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português - * para maiores detalhes. - * - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título - * "LICENCA.txt", junto com esse programa. Se não, acesse - * ou escreva para a Fundação do Software Livre (FSF) Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. - */ -package br.gov.frameworkdemoiselle.management.notification; - -import java.io.Serializable; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Event; -import javax.enterprise.event.Observes; -import javax.enterprise.util.AnnotationLiteral; -import javax.inject.Inject; - -import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; -import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.AttributeChange; -import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.Generic; -import br.gov.frameworkdemoiselle.util.Beans; - -/** - * - *

Central class to manage sending notifications to management clients. - * This class allows applications to send management notifications without - * knowledge of the technology used to send those notifications.

- * - *

To obtain an instance of the {@link NotificationManager} simply inject it in - * your code using {@link Inject} or the {@link Beans#getReference(Class beanType)} method. The {@link NotificationManager} - * is {@link ApplicationScoped}, so you can inject it as many times as needed and still have only one instance per application.

- * - *

Implementators of management protocols must observe the {@link NotificationEvent} event (using the {@link Observes} annotation), this way - * they will receive an event containing the original notification and can translate this notification to a specific protocol. Optionaly, - * the implementator can use qualifiers like the {@link Generic} and {@link AttributeChange} qualifiers - * to filter what king of notifications they will handle. One example of an implementator is the demoiselle-jmx extension.

- * - * @author serpro - * - */ -@ApplicationScoped -@SuppressWarnings("serial") -public class NotificationManager implements Serializable{ - - @Inject - @Generic - private Event genericNotificationEvent; - - @Inject - @AttributeChange - private Event attributeChangeNotificationEvent; - - /** - * Sends a generic notification to all management clients. - * - * @param notification The notification to send - */ - public void sendNotification(Notification notification) { - if (! AttributeChangeNotification.class.isInstance(notification) ){ - getGenericNotificationEvent().fire(new NotificationEvent(notification)); - } - else{ - getAttributeChangeNotificationEvent().fire(new NotificationEvent(notification)); - } - } - - @SuppressWarnings("unchecked") - private Event getGenericNotificationEvent() { - if (genericNotificationEvent==null){ - genericNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral() {}); - } - - return genericNotificationEvent; - } - - @SuppressWarnings("unchecked") - private Event getAttributeChangeNotificationEvent() { - if (attributeChangeNotificationEvent==null){ - attributeChangeNotificationEvent = Beans.getReference(Event.class , new AnnotationLiteral() {}); - } - - return attributeChangeNotificationEvent; - } - - - -} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/stereotype/ManagementController.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/stereotype/ManagementController.java new file mode 100644 index 0000000..9f2e7d6 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/stereotype/ManagementController.java @@ -0,0 +1,79 @@ +/* + * Demoiselle Framework + * Copyright (C) 2011 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.stereotype; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Stereotype; +import javax.enterprise.util.Nonbinding; + +/** + *

Identifies a management controller class. What it means is that an external client can manage the application + * this class is in by reading or writing it's attributes and calling it's operations.

+ *

+ * Only fields annotated with {@link br.gov.frameworkdemoiselle.annotation.ManagedProperty} or + * methods annotated with {@link br.gov.frameworkdemoiselle.annotation.ManagedOperation} will be exposed + * to clients.

+ *

This stereotype only defines a class as managed, you need to choose an extension that will expose this managed class + * to external clients using any technology available. One example is the Demoiselle JMX extension, that will expose + * managed classes as MBeans.

+ * + * @author SERPRO + */ +@ApplicationScoped +@Stereotype +@Documented +@Controller +@Inherited +@Retention(RUNTIME) +@Target({ TYPE }) +public @interface ManagementController { + + /** + * @return Human readable description of this managed class. + */ + @Nonbinding + String description() default ""; + +} diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/AllowedValues.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/AllowedValues.java new file mode 100644 index 0000000..3a8983f --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/validation/AllowedValues.java @@ -0,0 +1,76 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.validation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; + +import br.gov.frameworkdemoiselle.internal.validation.AllowedValuesValidator; + +@Documented +@Target({ FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = AllowedValuesValidator.class) +/** + * Validate a value against a list of allowed values. + * + * @author serpro + * + */ +public @interface AllowedValues { + + /** + * List of accepted values + */ + String[] allows(); + + /** + * Type of allowed values. Defaults to {@link ValueType#STRING}. + */ + ValueType valueType() default ValueType.STRING; + + enum ValueType { + STRING,INTEGER,DECIMAL; + } + +} diff --git a/impl/core/src/test/java/management/ManagementBootstrapTestCase.java b/impl/core/src/test/java/management/ManagementBootstrapTestCase.java index eb45cb2..941d326 100644 --- a/impl/core/src/test/java/management/ManagementBootstrapTestCase.java +++ b/impl/core/src/test/java/management/ManagementBootstrapTestCase.java @@ -56,8 +56,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import test.LocaleProducer; -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; -import br.gov.frameworkdemoiselle.management.internal.ManagedType; +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.lifecycle.ManagementExtension; import br.gov.frameworkdemoiselle.util.Beans; @RunWith(Arquillian.class) @@ -111,7 +111,7 @@ public class ManagementBootstrapTestCase { deployer.deploy("default"); // "store" é application scoped e é usado pelo DummyManagementExtension para - // armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, + // armazenar todos os beans anotados com @ManagementController. Se o bootstrap rodou corretamente, // ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. ManagedClassStore store = Beans.getReference(ManagedClassStore.class); @@ -129,7 +129,7 @@ public class ManagementBootstrapTestCase { deployer.deploy("default"); // "store" é application scoped e é usado pelo DummyManagementExtension para - // armazenar todos os beans anotados com @Managed. Se o bootstrap rodou corretamente, + // armazenar todos os beans anotados com @ManagementController. Se o bootstrap rodou corretamente, // ele chamou DummyManagementExtension.initialize e este store conterá o bean de teste que anotamos. // Nós então disparamos o evento de shutdown onde ele deverá limpar o store. ManagedClassStore store = Beans.getReference(ManagedClassStore.class); diff --git a/impl/core/src/test/java/management/ManagementTestCase.java b/impl/core/src/test/java/management/ManagementTestCase.java index 81fc781..a6dfbc6 100644 --- a/impl/core/src/test/java/management/ManagementTestCase.java +++ b/impl/core/src/test/java/management/ManagementTestCase.java @@ -152,7 +152,7 @@ public class ManagementTestCase { try { // O método "nonOperationAnnotatedMethod" existe na classe DummyManagedClass, mas não está anotado como - // "@Operation", então + // "@ManagedOperation", então // ela não pode ser exposta para extensões. store.invoke(DummyManagedClass.class, "nonOperationAnnotatedMethod"); Assert.fail(); diff --git a/impl/core/src/test/java/management/NotificationTestCase.java b/impl/core/src/test/java/management/NotificationTestCase.java index 9025228..6108056 100644 --- a/impl/core/src/test/java/management/NotificationTestCase.java +++ b/impl/core/src/test/java/management/NotificationTestCase.java @@ -54,11 +54,11 @@ import org.junit.runner.RunWith; import test.LocaleProducer; import br.gov.frameworkdemoiselle.annotation.Name; -import br.gov.frameworkdemoiselle.management.internal.ManagedType; -import br.gov.frameworkdemoiselle.management.internal.MonitoringManager; -import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; -import br.gov.frameworkdemoiselle.management.notification.Notification; -import br.gov.frameworkdemoiselle.management.notification.NotificationManager; +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.internal.management.Management; +import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; +import br.gov.frameworkdemoiselle.management.Notification; +import br.gov.frameworkdemoiselle.management.NotificationManager; import br.gov.frameworkdemoiselle.util.Beans; import br.gov.frameworkdemoiselle.util.ResourceBundle; @@ -119,7 +119,7 @@ public class NotificationTestCase { */ @Test public void testNotifyChangeManagedClass(){ - MonitoringManager manager = Beans.getReference(MonitoringManager.class); + Management manager = Beans.getReference(Management.class); for (ManagedType type : manager.getManagedTypes()){ if (type.getType().equals(DummyManagedClass.class)){ diff --git a/impl/core/src/test/java/management/testclasses/DummyManagedClass.java b/impl/core/src/test/java/management/testclasses/DummyManagedClass.java index b2717f6..1577842 100644 --- a/impl/core/src/test/java/management/testclasses/DummyManagedClass.java +++ b/impl/core/src/test/java/management/testclasses/DummyManagedClass.java @@ -38,44 +38,44 @@ package management.testclasses; import java.util.UUID; -import br.gov.frameworkdemoiselle.management.annotation.Managed; -import br.gov.frameworkdemoiselle.management.annotation.Operation; -import br.gov.frameworkdemoiselle.management.annotation.Property; -import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues; -import br.gov.frameworkdemoiselle.management.annotation.validation.AllowedValues.ValueType; +import br.gov.frameworkdemoiselle.annotation.ManagedOperation; +import br.gov.frameworkdemoiselle.annotation.ManagedProperty; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; +import br.gov.frameworkdemoiselle.validation.AllowedValues; +import br.gov.frameworkdemoiselle.validation.AllowedValues.ValueType; -@Managed +@ManagementController public class DummyManagedClass { - @Property + @ManagedProperty private String name; - @Property + @ManagedProperty @AllowedValues(allows={"f","m","F","M"},valueType=ValueType.INTEGER) private Integer id; - @Property + @ManagedProperty private Integer firstFactor , secondFactor; - @Property + @ManagedProperty private String uuid; - @Property + @ManagedProperty private String writeOnlyProperty; - @Property + @ManagedProperty private String readOnlyProperty = "Default Value"; /** * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas uma letra. */ - @Property + @ManagedProperty private Integer a; /** * Propriedade para testar detecção de métodos GET e SET quando propriedade tem apenas letras maiúsculas. */ - @Property + @ManagedProperty private Integer MAIUSCULO; public Integer getId() { @@ -111,7 +111,7 @@ public class DummyManagedClass { MAIUSCULO = mAIUSCULO; } - @Operation(description="Generates a random UUID") + @ManagedOperation(description="Generates a random UUID") public String generateUUID(){ this.uuid = UUID.randomUUID().toString(); return this.uuid; @@ -152,7 +152,7 @@ public class DummyManagedClass { this.secondFactor = secondFactor; } - @Operation + @ManagedOperation public Integer calculateFactorsNonSynchronized(Integer firstFactor , Integer secondFactor){ setFirstFactor(firstFactor); setSecondFactor(secondFactor); @@ -173,7 +173,7 @@ public class DummyManagedClass { } } - @Operation + @ManagedOperation public synchronized Integer calculateFactorsSynchronized(Integer firstFactor , Integer secondFactor){ setFirstFactor(firstFactor); setSecondFactor(secondFactor); diff --git a/impl/core/src/test/java/management/testclasses/DummyManagedClassPropertyError.java b/impl/core/src/test/java/management/testclasses/DummyManagedClassPropertyError.java index 818e563..ad0ae1f 100644 --- a/impl/core/src/test/java/management/testclasses/DummyManagedClassPropertyError.java +++ b/impl/core/src/test/java/management/testclasses/DummyManagedClassPropertyError.java @@ -36,8 +36,8 @@ */ package management.testclasses; -import br.gov.frameworkdemoiselle.management.annotation.Managed; -import br.gov.frameworkdemoiselle.management.annotation.Property; +import br.gov.frameworkdemoiselle.annotation.ManagedProperty; +import br.gov.frameworkdemoiselle.stereotype.ManagementController; /** @@ -47,13 +47,13 @@ import br.gov.frameworkdemoiselle.management.annotation.Property; * @author serpro * */ -@Managed +@ManagementController public class DummyManagedClassPropertyError { /** - * Property with no setter or getter + * ManagedProperty with no setter or getter */ - @Property + @ManagedProperty private Long property; } diff --git a/impl/core/src/test/java/management/testclasses/DummyManagementExtension.java b/impl/core/src/test/java/management/testclasses/DummyManagementExtension.java index 346f157..8ac61e1 100644 --- a/impl/core/src/test/java/management/testclasses/DummyManagementExtension.java +++ b/impl/core/src/test/java/management/testclasses/DummyManagementExtension.java @@ -41,8 +41,8 @@ import java.util.List; import javax.inject.Inject; -import br.gov.frameworkdemoiselle.management.extension.ManagementExtension; -import br.gov.frameworkdemoiselle.management.internal.ManagedType; +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.lifecycle.ManagementExtension; public class DummyManagementExtension implements ManagementExtension { diff --git a/impl/core/src/test/java/management/testclasses/DummyNotificationListener.java b/impl/core/src/test/java/management/testclasses/DummyNotificationListener.java index bb9f701..6b4d8ec 100644 --- a/impl/core/src/test/java/management/testclasses/DummyNotificationListener.java +++ b/impl/core/src/test/java/management/testclasses/DummyNotificationListener.java @@ -39,11 +39,11 @@ package management.testclasses; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; -import br.gov.frameworkdemoiselle.management.internal.notification.event.NotificationEvent; -import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.AttributeChange; -import br.gov.frameworkdemoiselle.management.internal.notification.qualifier.Generic; -import br.gov.frameworkdemoiselle.management.notification.AttributeChangeNotification; -import br.gov.frameworkdemoiselle.management.notification.NotificationManager; +import br.gov.frameworkdemoiselle.internal.management.ManagementNotificationEvent; +import br.gov.frameworkdemoiselle.internal.management.qualifier.AttributeChange; +import br.gov.frameworkdemoiselle.internal.management.qualifier.Generic; +import br.gov.frameworkdemoiselle.management.AttributeChangeNotification; +import br.gov.frameworkdemoiselle.management.NotificationManager; /** * Dummy class to test receiving of notifications sent by the {@link NotificationManager} @@ -56,11 +56,11 @@ public class DummyNotificationListener { private String message = null; - public void listenNotification(@Observes @Generic NotificationEvent event){ + public void listenNotification(@Observes @Generic ManagementNotificationEvent event){ message = event.getNotification().getMessage().toString(); } - public void listenAttributeChangeNotification(@Observes @AttributeChange NotificationEvent event){ + public void listenAttributeChangeNotification(@Observes @AttributeChange ManagementNotificationEvent event){ AttributeChangeNotification notification = (AttributeChangeNotification)event.getNotification(); message = notification.getMessage().toString() + " - " + notification.getAttributeName(); } diff --git a/impl/core/src/test/java/management/testclasses/ManagedClassStore.java b/impl/core/src/test/java/management/testclasses/ManagedClassStore.java index 56d734e..cd93411 100644 --- a/impl/core/src/test/java/management/testclasses/ManagedClassStore.java +++ b/impl/core/src/test/java/management/testclasses/ManagedClassStore.java @@ -42,8 +42,8 @@ import java.util.List; import javax.enterprise.context.ApplicationScoped; -import br.gov.frameworkdemoiselle.management.internal.ManagedType; -import br.gov.frameworkdemoiselle.management.internal.MonitoringManager; +import br.gov.frameworkdemoiselle.internal.management.ManagedType; +import br.gov.frameworkdemoiselle.internal.management.Management; import br.gov.frameworkdemoiselle.util.Beans; /** @@ -69,7 +69,7 @@ public class ManagedClassStore { } public void setProperty(Class managedClass , String attributeName , Object newValue){ - MonitoringManager manager = Beans.getReference(MonitoringManager.class); + Management manager = Beans.getReference(Management.class); for (ManagedType type : manager.getManagedTypes()){ if (type.getType().equals(managedClass)){ manager.setProperty(type, attributeName, newValue); @@ -79,7 +79,7 @@ public class ManagedClassStore { } public Object getProperty(Class managedClass , String attributeName ){ - MonitoringManager manager = Beans.getReference(MonitoringManager.class); + Management manager = Beans.getReference(Management.class); for (ManagedType type : manager.getManagedTypes()){ if (type.getType().equals(managedClass)){ return manager.getProperty(type, attributeName); @@ -90,7 +90,7 @@ public class ManagedClassStore { } public Object invoke(Class managedClass , String operation , Object... params){ - MonitoringManager manager = Beans.getReference(MonitoringManager.class); + Management manager = Beans.getReference(Management.class); for (ManagedType type : manager.getManagedTypes()){ if (type.getType().equals(managedClass)){ return manager.invoke(type, operation, params); -- libgit2 0.21.2