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    
025    /**
026     * Describes an attibute of a GBean.
027     *
028     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
029     */
030    public class GAttributeInfo implements Serializable {
031        private static final long serialVersionUID = 2805493042418685048L;
032    
033        /**
034         * Name of this attribute.
035         */
036        private final String name;
037    
038        /**
039         * Type of this attribute.
040         */
041        private final String type;
042    
043        /**
044         * Is this attribute persistent?
045         */
046        private final boolean persistent;
047    
048        /**
049         * Is this attribute manageable?
050         */
051        private final boolean manageable;
052    
053        /**
054         * Is this attribute readable?
055         */
056        private final boolean readable;
057    
058        /**
059         * Is this attribute writiable?
060         */
061        private final boolean writable;
062    
063        /**
064         * Name of the getter method.
065         * The default is "get" + name.  In the case of a defualt value we do a caseless search for the name.
066         */
067        private final String getterName;
068    
069        /**
070         * Name of the setter method.
071         * The default is "set" + name.  In the case of a defualt value we do a caseless search for the name.
072         */
073        private final String setterName;
074    
075        public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, String getterName, String setterName) {
076            this(name, type, persistent, manageable, getterName != null, setterName != null, getterName, setterName);
077        }
078    
079        public GAttributeInfo(String name, String type, boolean persistent, boolean manageable, boolean readable, boolean writable, String getterName, String setterName) {
080            this.name = name;
081            this.type = type;
082            this.persistent = persistent;
083            //non persistent attributes cannot be manageable
084            this.manageable = manageable & persistent;
085            this.readable = readable;
086            this.writable = writable;
087            this.getterName = getterName;
088            this.setterName = setterName;
089        }
090    
091        public String getName() {
092            return name;
093        }
094    
095        public String getType() {
096            return type;
097        }
098    
099        public boolean isPersistent() {
100            return persistent;
101        }
102    
103        public boolean isManageable() {
104            return manageable;
105        }
106    
107        public boolean isReadable() {
108            return readable;
109        }
110    
111        public boolean isWritable() {
112            return writable;
113        }
114    
115        public String getGetterName() {
116            return getterName;
117        }
118    
119        public String getSetterName() {
120            return setterName;
121        }
122    
123        public String toString() {
124            return "[GAttributeInfo: name=" + name +
125                     " type=" + type +
126                     " persistent=" + persistent +
127                     " manageable=" + manageable +
128                     " readable=" + readable +
129                     " writable=" + writable +
130                     " getterName=" + getterName +
131                     " setterName=" + setterName +
132                     "]";
133        }
134    
135        public String toXML(AbstractName abstractName) {
136            StringBuilder xml = new StringBuilder();
137    
138            xml.append("<gAttributeInfo ");
139            xml.append("name='" + name + "' ");
140            xml.append("type='" + type + "' ");
141            xml.append("persistent='" + persistent + "' ");
142            xml.append("manageable='" + manageable + "' ");
143            xml.append("readable='" + readable + "' ");
144            xml.append("writable='" + writable + "' ");
145            xml.append(">");
146    
147            xml.append("<getterName>" + getterName + "</getterName>");
148            xml.append("<setterName>" + setterName + "</setterName>");
149    
150            if (readable) {
151                try {
152                    Object value = KernelRegistry.getSingleKernel().getAttribute(abstractName, name);
153                    if (value != null) {
154                        if (value instanceof String[]) {
155                            for (String valueString : Arrays.asList((String[]) value))
156                                xml.append("<value>" + valueString + "</value>");
157                        } else {
158                            xml.append("<value>" + value + "</value>");
159                        }
160                    }
161                } catch (Exception e) {
162    
163                }
164            }
165    
166            xml.append("</gAttributeInfo>");
167    
168            return xml.toString();
169        }
170    }