/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/subsystem/CacheFactoryResourceDefinition.java
Java | 136 lines | 79 code | 16 blank | 41 comment | 4 complexity | d38a501338dc1bb4dfc02a5ca25f4a1d MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22package org.jboss.as.ejb3.subsystem;
23
24import java.util.Locale;
25import java.util.ResourceBundle;
26
27import javax.xml.stream.XMLStreamException;
28import javax.xml.stream.XMLStreamWriter;
29
30import org.jboss.as.controller.AttributeDefinition;
31import org.jboss.as.controller.ListAttributeDefinition;
32import org.jboss.as.controller.PathElement;
33import org.jboss.as.controller.ReloadRequiredWriteAttributeHandler;
34import org.jboss.as.controller.SimpleAttributeDefinition;
35import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
36import org.jboss.as.controller.SimpleResourceDefinition;
37import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
38import org.jboss.as.controller.descriptions.ResourceDescriptionResolver;
39import org.jboss.as.controller.operations.validation.StringLengthValidator;
40import org.jboss.as.controller.registry.AttributeAccess;
41import org.jboss.as.controller.registry.ManagementResourceRegistration;
42import org.jboss.as.controller.registry.OperationEntry;
43import org.jboss.dmr.ModelNode;
44import org.jboss.dmr.ModelType;
45
46/**
47 * @author Paul Ferraro
48 */
49public class CacheFactoryResourceDefinition extends SimpleResourceDefinition {
50
51 public static final ListAttributeDefinition ALIASES = new StringListAttributeDefinition(EJB3SubsystemModel.ALIASES, EJB3SubsystemXMLAttribute.ALIASES.getLocalName(), true);
52 public static final SimpleAttributeDefinition PASSIVATION_STORE =
53 new SimpleAttributeDefinitionBuilder(EJB3SubsystemModel.PASSIVATION_STORE, ModelType.STRING, true)
54 .setXmlName(EJB3SubsystemXMLAttribute.PASSIVATION_STORE_REF.getLocalName())
55 .setAllowExpression(true)
56 .setFlags(AttributeAccess.Flag.RESTART_NONE)
57 .build();
58
59 private static final AttributeDefinition[] ATTRIBUTES = new AttributeDefinition[] { ALIASES, PASSIVATION_STORE };
60 private static final CacheFactoryAdd ADD_HANDLER = new CacheFactoryAdd(ATTRIBUTES);
61 private static final CacheFactoryRemove REMOVE_HANDLER = new CacheFactoryRemove(ADD_HANDLER);
62
63 public static final CacheFactoryResourceDefinition INSTANCE = new CacheFactoryResourceDefinition();
64
65 private CacheFactoryResourceDefinition() {
66 super(PathElement.pathElement(EJB3SubsystemModel.CACHE),
67 EJB3Extension.getResourceDescriptionResolver(EJB3SubsystemModel.CACHE),
68 ADD_HANDLER, REMOVE_HANDLER,
69 OperationEntry.Flag.RESTART_NONE, OperationEntry.Flag.RESTART_RESOURCE_SERVICES);
70 }
71
72 @Override
73 public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
74 ReloadRequiredWriteAttributeHandler handler = new ReloadRequiredWriteAttributeHandler(ATTRIBUTES);
75 for (AttributeDefinition attribute: ATTRIBUTES) {
76 resourceRegistration.registerReadWriteAttribute(attribute, null, handler);
77 }
78 }
79
80 private static class StringListAttributeDefinition extends ListAttributeDefinition {
81
82 /** {@inheritDoc} */
83 StringListAttributeDefinition(String name, String xmlName, boolean allowNull) {
84 super(name, xmlName, allowNull, 0, Integer.MAX_VALUE, new StringLengthValidator(1));
85 }
86
87 /**
88 * {@inheritDoc}
89 * @see org.jboss.as.controller.ListAttributeDefinition#addValueTypeDescription(org.jboss.dmr.ModelNode, java.util.ResourceBundle)
90 */
91 @Override
92 protected void addValueTypeDescription(ModelNode node, ResourceBundle bundle) {
93 this.setValueType(node);
94 }
95
96 /**
97 * {@inheritDoc}
98 * @see org.jboss.as.controller.ListAttributeDefinition#addAttributeValueTypeDescription(org.jboss.dmr.ModelNode, org.jboss.as.controller.descriptions.ResourceDescriptionResolver, java.util.Locale, java.util.ResourceBundle)
99 */
100 @Override
101 protected void addAttributeValueTypeDescription(ModelNode node, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
102 this.setValueType(node);
103 }
104
105 /**
106 * {@inheritDoc}
107 * @see org.jboss.as.controller.ListAttributeDefinition#addOperationParameterValueTypeDescription(org.jboss.dmr.ModelNode, java.lang.String, org.jboss.as.controller.descriptions.ResourceDescriptionResolver, java.util.Locale, java.util.ResourceBundle)
108 */
109 @Override
110 protected void addOperationParameterValueTypeDescription(ModelNode node, String operationName, ResourceDescriptionResolver resolver, Locale locale, ResourceBundle bundle) {
111 this.setValueType(node);
112 }
113
114 private void setValueType(ModelNode node) {
115 node.get(ModelDescriptionConstants.VALUE_TYPE).set(ModelType.STRING);
116 }
117
118 /**
119 * {@inheritDoc}
120 * @see org.jboss.as.controller.AttributeDefinition#marshallAsElement(org.jboss.dmr.ModelNode, javax.xml.stream.XMLStreamWriter)
121 */
122 @Override
123 public void marshallAsElement(ModelNode model, XMLStreamWriter writer) throws XMLStreamException {
124 if (model.hasDefined(this.getName())) {
125 StringBuilder builder = new StringBuilder();
126 for (ModelNode alias: model.get(this.getName()).asList()) {
127 if (builder.length() > 0) {
128 builder.append(' ');
129 }
130 builder.append(alias.asString());
131 }
132 writer.writeAttribute(this.getXmlName(), builder.toString());
133 }
134 }
135 }
136}