001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.monitoring;
018:
019: import java.lang.annotation.Annotation;
020:
021: import org.romaframework.aspect.core.annotation.AnnotationConstants;
022: import org.romaframework.aspect.monitoring.annotation.MonitoringAction;
023: import org.romaframework.aspect.monitoring.annotation.MonitoringClass;
024: import org.romaframework.aspect.monitoring.annotation.MonitoringField;
025: import org.romaframework.aspect.monitoring.feature.MonitoringFeatures;
026: import org.romaframework.core.aspect.SelfRegistrantConfigurableAspect;
027: import org.romaframework.core.schema.SchemaClassDefinition;
028: import org.romaframework.core.schema.SchemaElement;
029: import org.romaframework.core.schema.SchemaFeatures;
030: import org.romaframework.core.schema.SchemaField;
031: import org.romaframework.core.util.DynaBean;
032: import org.romaframework.xml.config.XmlConfigActionType;
033: import org.romaframework.xml.config.XmlConfigAspectActionTypeMonitoring;
034: import org.romaframework.xml.config.XmlConfigAspectClassTypeMonitoring;
035: import org.romaframework.xml.config.XmlConfigAspectFieldTypeMonitoring;
036: import org.romaframework.xml.config.XmlConfigClassType;
037: import org.romaframework.xml.config.XmlConfigFieldType;
038:
039: /**
040: * Extend this if you want to write your own Aspect implementation.
041: *
042: * @author Luca Garulli (luca.garulli@assetdata.it)
043: *
044: */
045: public abstract class MonitoringAspectAbstract extends
046: SelfRegistrantConfigurableAspect<String> implements
047: MonitoringAspect {
048:
049: @Override
050: public void configClass(SchemaClassDefinition iClass,
051: Annotation iAnnotation, XmlConfigClassType iXmlNode) {
052: DynaBean features = iClass.getFeatures(ASPECT_NAME);
053: if (features == null) {
054: // CREATE EMPTY FEATURES
055: features = new MonitoringFeatures();
056: iClass.setFeatures(ASPECT_NAME, features);
057: }
058:
059: readClassAnnotation(iAnnotation, features);
060: readClassXml(iClass, iXmlNode);
061: setDefaults(iClass);
062: }
063:
064: private void readClassAnnotation(Annotation iAnnotation,
065: DynaBean features) {
066: MonitoringClass annotation = (MonitoringClass) iAnnotation;
067:
068: if (annotation != null) {
069: // PROCESS ANNOTATIONS
070: if (annotation.enabled() != AnnotationConstants.UNSETTED)
071: features
072: .setAttribute(
073: MonitoringFeatures.ENABLED,
074: annotation.enabled() == AnnotationConstants.TRUE);
075: }
076: }
077:
078: private void readClassXml(SchemaClassDefinition iClass,
079: XmlConfigClassType iXmlNode) {
080: if (iXmlNode == null || iXmlNode.getAspects() == null)
081: return;
082:
083: DynaBean features = iClass.getFeatures(ASPECT_NAME);
084:
085: XmlConfigAspectClassTypeMonitoring featureDescriptor = iXmlNode
086: .getAspects().getMonitoring();
087:
088: if (featureDescriptor != null) {
089: // PROCESS DESCRIPTOR CFG
090: if (featureDescriptor != null) {
091: if (featureDescriptor.isSetEnabled())
092: features.setAttribute(MonitoringFeatures.ENABLED,
093: featureDescriptor.getEnabled());
094: }
095: }
096: }
097:
098: @Override
099: public void configField(SchemaField iField,
100: Annotation iFieldAnnotation, Annotation iGenericAnnotation,
101: Annotation iGetterAnnotation, XmlConfigFieldType iXmlNode) {
102: DynaBean features = iField.getFeatures(ASPECT_NAME);
103: if (features == null) {
104: // CREATE EMPTY FEATURES
105: features = new MonitoringFeatures();
106: iField.setFeatures(ASPECT_NAME, features);
107: }
108:
109: readFieldAnnotation(iField, iFieldAnnotation, features);
110: readFieldAnnotation(iField, iGetterAnnotation, features);
111: readFieldXml(iField, iXmlNode);
112: setDefaults(iField);
113: }
114:
115: private void readFieldAnnotation(SchemaField iField,
116: Annotation iAnnotation, DynaBean features) {
117: MonitoringField annotation = (MonitoringField) iAnnotation;
118:
119: if (annotation != null) {
120: // PROCESS ANNOTATIONS
121: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
122: if (annotation != null) {
123: if (annotation.enabled() != AnnotationConstants.UNSETTED)
124: features
125: .setAttribute(
126: MonitoringFeatures.ENABLED,
127: annotation.enabled() == AnnotationConstants.TRUE);
128: }
129: }
130: }
131:
132: private void readFieldXml(SchemaField iField,
133: XmlConfigFieldType iXmlNode) {
134: // PROCESS DESCRIPTOR CFG
135: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
136: // VALUES
137: if (iXmlNode == null || iXmlNode.getAspects() == null)
138: return;
139:
140: DynaBean features = iField.getFeatures(ASPECT_NAME);
141:
142: XmlConfigAspectFieldTypeMonitoring descriptor = iXmlNode
143: .getAspects().getMonitoring();
144:
145: if (descriptor != null) {
146: if (descriptor.isSetEnabled())
147: features.setAttribute(MonitoringFeatures.ENABLED,
148: descriptor.getEnabled());
149: }
150: }
151:
152: @Override
153: public void configAction(SchemaElement iAction,
154: Annotation iActionAnnotation,
155: Annotation iGenericAnnotation, XmlConfigActionType iXmlNode) {
156: DynaBean features = iAction.getFeatures(ASPECT_NAME);
157: if (features == null) {
158: // CREATE EMPTY FEATURES
159: features = new MonitoringFeatures();
160: iAction.setFeatures(ASPECT_NAME, features);
161: }
162:
163: readActionAnnotation(iAction, iActionAnnotation, features);
164: readActionXml(iAction, iXmlNode);
165: setDefaults(iAction);
166: }
167:
168: private void readActionAnnotation(SchemaElement iAction,
169: Annotation iAnnotation, DynaBean features) {
170: MonitoringAction annotation = (MonitoringAction) iAnnotation;
171:
172: if (annotation != null) {
173: // PROCESS ANNOTATIONS
174: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
175: if (annotation != null) {
176: if (annotation.enabled() != AnnotationConstants.UNSETTED)
177: features
178: .setAttribute(
179: MonitoringFeatures.ENABLED,
180: annotation.enabled() == AnnotationConstants.TRUE);
181: }
182: }
183: }
184:
185: private void readActionXml(SchemaElement iAction,
186: XmlConfigActionType iXmlNode) {
187: // PROCESS DESCRIPTOR CFG
188: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
189: // VALUES
190: if (iXmlNode == null || iXmlNode.getAspects() == null)
191: return;
192:
193: DynaBean features = iAction.getFeatures(ASPECT_NAME);
194:
195: XmlConfigAspectActionTypeMonitoring descriptor = iXmlNode
196: .getAspects().getMonitoring();
197:
198: if (descriptor != null) {
199: if (descriptor.isSetEnabled())
200: features.setAttribute(MonitoringFeatures.ENABLED,
201: descriptor.getEnabled());
202: }
203: }
204:
205: private void setDefaults(SchemaFeatures iElement) {
206: DynaBean features = iElement.getFeatures(ASPECT_NAME);
207:
208: if (features.getAttribute(MonitoringFeatures.ENABLED) == null)
209: features.setAttribute(MonitoringFeatures.ENABLED, true);
210: }
211:
212: public String aspectName() {
213: return ASPECT_NAME;
214: }
215: }
|