001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.gbean;
019
020 import java.io.Serializable;
021 import java.util.Arrays;
022
023 import org.apache.geronimo.kernel.KernelRegistry;
024 import org.apache.geronimo.crypto.EncryptionManager;
025
026 /**
027 * Describes an attibute of a GBean.
028 *
029 * @version $Rev: 959940 $ $Date: 2010-07-02 18:25:14 +0800 (Fri, 02 Jul 2010) $
030 */
031 public class GAttributeInfo implements Serializable {
032 private static final long serialVersionUID = 2805493042418685048L;
033
034 /**
035 * Name of this attribute.
036 */
037 private final String name;
038
039 /**
040 * Type of this attribute.
041 */
042 private final String type;
043
044 /**
045 * Is this attribute persistent?
046 */
047 private final boolean persistent;
048
049 /**
050 * Is this attribute manageable?
051 */
052 private final boolean manageable;
053
054 /**
055 * Does this attribute need to be encrypted when persisted?
056 */
057 private final boolean encrypted;
058
059 /**
060 * Is this attribute readable?
061 */
062 private final boolean readable;
063
064 /**
065 * Is this attribute writiable?
066 */
067 private final boolean writable;
068
069 /**
070 * Name of the getter method.
071 * The default is "get" + name. In the case of a defualt value we do a caseless search for the name.
072 */
073 private final String getterName;
074
075 /**
076 * Name of the setter method.
077 * The default is "set" + name. In the case of a defualt value we do a caseless search for the name.
078 */
079 private final String setterName;
080
081 /**
082 * Determines whether the given attribute need to be encrypted by default.
083 *
084 * @param name - Name of the attribute
085 * @param type - Type of the attribute
086 * @return
087 */
088 private static boolean defaultEncrypted(String name, String type) {
089 if (name != null && (name.toLowerCase().contains("password") || name.toLowerCase().contains("keystorepass") || name.toLowerCase().contains("truststorepass")) && "java.lang.String".equals(type)) {
090 return true;
091 } else {
092 return false;
093 }
094 }
095
096 public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, String getterName, String setterName) {
097 this(name, type, persistent, manageable, getterName != null, setterName != null, getterName, setterName);
098 }
099
100 public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, boolean readable, boolean writable, String getterName, String setterName) {
101 this(name, type, persistent, manageable, defaultEncrypted(name, type), readable, writable, getterName,
102 setterName);
103 }
104
105 public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, boolean encrypted, String getterName, String setterName) {
106 this(name, type, persistent, manageable, encrypted, getterName != null, setterName != null, getterName,
107 setterName);
108 }
109
110 public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, boolean encrypted, boolean readable, boolean writable, String getterName, String setterName) {
111 if (encrypted && !"java.lang.String".equals(type)) {
112 throw new IllegalArgumentException("Only attributes of String type can be encrypted.");
113 }
114 this.name = name;
115 this.type = type;
116 this.persistent = persistent;
117 //non persistent attributes cannot be manageable
118 this.manageable = manageable & persistent;
119 this.encrypted = encrypted;
120 this.readable = readable;
121 this.writable = writable;
122 this.getterName = getterName;
123 this.setterName = setterName;
124 }
125
126 public String getName() {
127 return name;
128 }
129
130 public String getType() {
131 return type;
132 }
133
134 public boolean isPersistent() {
135 return persistent;
136 }
137
138 public boolean isManageable() {
139 return manageable;
140 }
141
142 public boolean isEncrypted() {
143 return encrypted;
144 }
145
146 public boolean isReadable() {
147 return readable;
148 }
149
150 public boolean isWritable() {
151 return writable;
152 }
153
154 public String getGetterName() {
155 return getterName;
156 }
157
158 public String getSetterName() {
159 return setterName;
160 }
161
162 public String toString() {
163 return "[GAttributeInfo: name=" + name +
164 " type=" + type +
165 " persistent=" + persistent +
166 " manageable=" + manageable +
167 " encrypted=" + encrypted +
168 " readable=" + readable +
169 " writable=" + writable +
170 " getterName=" + getterName +
171 " setterName=" + setterName +
172 "]";
173 }
174
175 public String toXML(AbstractName abstractName) {
176 StringBuilder xml = new StringBuilder();
177
178 xml.append("<gAttributeInfo ");
179 xml.append("name='" + name + "' ");
180 xml.append("type='" + type + "' ");
181 xml.append("persistent='" + persistent + "' ");
182 xml.append("manageable='" + manageable + "' ");
183 xml.append("encrypted='" + encrypted + "' ");
184 xml.append("readable='" + readable + "' ");
185 xml.append("writable='" + writable + "' ");
186 xml.append(">");
187
188 xml.append("<getterName>" + getterName + "</getterName>");
189 xml.append("<setterName>" + setterName + "</setterName>");
190
191 if (readable) {
192 try {
193 Object value = KernelRegistry.getSingleKernel().getAttribute(abstractName, name);
194 if (value != null) {
195 if (value instanceof String[]) {
196 for (String valueString : Arrays.asList((String[]) value)) {
197 xml.append("<value>" + valueString + "</value>");
198 }
199 } else {
200 if (encrypted && value instanceof String) {
201 value = EncryptionManager.encrypt((String) value);
202 }
203 xml.append("<value>" + value + "</value>");
204 }
205 }
206 } catch (Exception e) {
207
208 }
209 }
210
211 xml.append("</gAttributeInfo>");
212
213 return xml.toString();
214 }
215 }