/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/operations/common/ProcessEnvironment.java
Java | 186 lines | 86 code | 28 blank | 72 comment | 12 complexity | 18339de8f10c455b044f40ceeb6cbbd2 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 */
22
23package org.jboss.as.controller.operations.common;
24
25import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
26
27import java.net.InetAddress;
28import java.net.UnknownHostException;
29import java.util.UUID;
30
31import org.jboss.as.controller.AttributeDefinition;
32import org.jboss.as.controller.ControlledProcessState;
33import org.jboss.as.controller.ControllerMessages;
34import org.jboss.as.controller.OperationContext;
35import org.jboss.as.controller.OperationFailedException;
36import org.jboss.as.controller.OperationStepHandler;
37import org.jboss.as.controller.PathAddress;
38import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
39import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
40import org.jboss.as.controller.interfaces.InetAddressUtil;
41import org.jboss.dmr.ModelNode;
42import org.jboss.dmr.ModelType;
43
44/**
45 * Base class for objects that store environment information for a process.
46 *
47 * @author Brian Stansberry (c) 2011 Red Hat Inc.
48 */
49public abstract class ProcessEnvironment {
50
51 /** The special process name value that triggers calculation of a UUID */
52 public static final String JBOSS_DOMAIN_UUID = "jboss.domain.uuid";
53
54 /** {@link AttributeDefinition} for the {@code name} attribute for a processes root resource */
55 public static final AttributeDefinition NAME = new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.NAME, ModelType.STRING, true)
56 .setAllowExpression(true).build();
57
58 /**
59 * Gets an {@link OperationStepHandler} that can read the {@code name} attribute for a processes root resource
60 * @return the handler
61 */
62 public OperationStepHandler getProcessNameReadHandler() {
63 return new ProcessNameReadAttributeHandler();
64 }
65
66 /**
67 * Gets an {@link OperationStepHandler} that can write the {@code name} attribute for a processes root resource
68 * @return the handler
69 */
70 public OperationStepHandler getProcessNameWriteHandler() {
71 return new ProcessNameWriteAttributeHandler();
72 }
73
74 /**
75 * Gets the resolved name of this process; a value previously passed to {@link #setProcessName(String)} or
76 * a value derived from the environment.
77 *
78 * @return the process name. Cannot be {@code null}
79 */
80 protected abstract String getProcessName();
81
82 /**
83 * Sets the process name. This method can only be called by the handler returned by
84 * {@link #getProcessNameWriteHandler()}; its visibility is protected only because subclasses need to implement it.
85 *
86 * @param processName the process name. May be {@code null} in which case a default process name should be used.
87 */
88 protected abstract void setProcessName(String processName);
89
90 /**
91 * Gets whether updating the runtime system properties with the given property is allowed.
92 *
93 * @param propertyName the name of the property. Cannot be {@code null}
94 * @param propertyValue the value of the property. May be {@code null}
95 * @param bootTime {@code true} if the process is currently booting
96 *
97 * @return {@code true} if the update can be applied to the runtime system properties; {@code} false if it
98 * should just be stored in the persistent configuration and the process should be put into
99 * {@link ControlledProcessState.State#RELOAD_REQUIRED reload-required state}.
100 *
101 * @throws OperationFailedException if a change to the given property is not allowed at all; e.g. changing
102 * {@code jboss.server.base.dir} after primordial boot is not allowed; the
103 * property can only be set from the command line
104 */
105 protected abstract boolean isRuntimeSystemPropertyUpdateAllowed(String propertyName,
106 String propertyValue,
107 boolean bootTime) throws OperationFailedException;
108
109 /**
110 * Notifies this {@code ProcessEnvironment} that the runtime value of the given system property has been updated,
111 * allowing it to update any state that was originally set via the system property during primordial process boot.
112 * This method should only be invoked after a call to {@link #isRuntimeSystemPropertyUpdateAllowed(String, String, boolean)}
113 * has returned {@code true}.
114 *
115 * @param propertyName the name of the property. Cannot be {@code null}
116 * @param propertyValue the value of the property. May be {@code null}
117 */
118 protected abstract void systemPropertyUpdated(String propertyName, String propertyValue);
119
120 protected static String resolveGUID(final String unresolvedName) {
121
122 String result;
123
124 if (JBOSS_DOMAIN_UUID.equals(unresolvedName)) {
125 try {
126 InetAddress localhost = InetAddressUtil.getLocalHost();
127 result = UUID.nameUUIDFromBytes(localhost.getAddress()).toString();
128 } catch (UnknownHostException e) {
129 throw ControllerMessages.MESSAGES.cannotResolveProcessUUID(e);
130 }
131 } else {
132 result = unresolvedName;
133 }
134
135 return result;
136 }
137
138 private class ProcessNameWriteAttributeHandler implements OperationStepHandler {
139
140 @Override
141 public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
142
143 final ModelNode model = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS).getModel();
144
145 final ModelNode newValue = operation.hasDefined(VALUE) ? operation.get(VALUE) : new ModelNode();
146 final ModelNode mockOp = new ModelNode();
147 mockOp.get(NAME.getName()).set(newValue);
148
149 NAME.validateAndSet(mockOp, model);
150
151 boolean booting = context.isBooting();
152 String resolved = null;
153 if (context.isBooting()) {
154 final ModelNode resolvedNode = NAME.resolveModelAttribute(context, model);
155 resolved = resolvedNode.isDefined() ? resolvedNode.asString() : null;
156 resolved = resolved == null ? null : resolveGUID(resolved);
157 } else {
158 context.reloadRequired();
159 }
160
161 if (context.completeStep() == OperationContext.ResultAction.KEEP) {
162 if (booting) {
163 ProcessEnvironment.this.setProcessName(resolved);
164 }
165 } else if (!booting) {
166 context.revertReloadRequired();
167 }
168 }
169 }
170
171 private class ProcessNameReadAttributeHandler implements OperationStepHandler {
172
173 @Override
174 public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
175
176 final ModelNode model = context.readResource(PathAddress.EMPTY_ADDRESS).getModel();
177 if (model.hasDefined(NAME.getName())) {
178 context.getResult().set(model.get(NAME.getName()));
179 } else {
180 context.getResult().set(ProcessEnvironment.this.getProcessName());
181 }
182
183 context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
184 }
185 }
186}