001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CommonAnnotationMetadata.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.metadata;
025:
026: import java.util.Arrays;
027:
028: import org.ow2.easybeans.deployment.annotations.impl.JAnnotationResource;
029: import org.ow2.easybeans.deployment.annotations.impl.JEjbEJB;
030: import org.ow2.easybeans.deployment.annotations.impl.JavaxPersistenceContext;
031: import org.ow2.easybeans.deployment.annotations.impl.JavaxPersistenceUnit;
032: import org.ow2.easybeans.deployment.annotations.metadata.interfaces.ISharedMetadata;
033:
034: /**
035: * Defines Metadata shared by Field, Method and Classes. For example
036: * @javax.annotation.EJB, @javax.annotation.Resource, etc.
037: * @author Florent Benoit
038: */
039: public class CommonAnnotationMetadata implements ISharedMetadata {
040:
041: /**
042: * This field is used as a PersistenceContext.
043: */
044: private JavaxPersistenceContext javaxPersistenceContext = null;
045:
046: /**
047: * This field is used as a PersistenceUnit.
048: */
049: private JavaxPersistenceUnit javaxPersistenceUnit = null;
050:
051: /**
052: * Object representing @{@link javax.ejb.EJB} annotation.
053: */
054: private JEjbEJB jEjbEJB = null;
055:
056: /**
057: * Object representing @{@link javax.annotation.Resource} annotation.
058: */
059: private JAnnotationResource jAnnotationResource = null;
060:
061: /**
062: * @return JEjbEJB object representing javax.ejb.EJB annotation.
063: */
064: public JEjbEJB getJEjbEJB() {
065: return jEjbEJB;
066: }
067:
068: /**
069: * Set JAnnotationEJB object.
070: * @param jEjbEJB object representing javax.annotation.EJB annotation.
071: */
072: public void setJEjbEJB(final JEjbEJB jEjbEJB) {
073: this .jEjbEJB = jEjbEJB;
074: }
075:
076: /**
077: * @return JAnnotationResource object representing javax.annotation.Resource
078: * annotation.
079: */
080: public JAnnotationResource getJAnnotationResource() {
081: return jAnnotationResource;
082: }
083:
084: /**
085: * Set JAnnotationResource object.
086: * @param jAnnotationResource object representing javax.annotation.Resource
087: * annotation.
088: */
089: public void setJAnnotationResource(
090: final JAnnotationResource jAnnotationResource) {
091: this .jAnnotationResource = jAnnotationResource;
092: }
093:
094: /**
095: * @return true if this field is used as a persistence context.
096: */
097: public boolean isPersistenceContext() {
098: return javaxPersistenceContext != null;
099: }
100:
101: /**
102: * @return the persistence context infos.
103: */
104: public JavaxPersistenceContext getJavaxPersistenceContext() {
105: return javaxPersistenceContext;
106: }
107:
108: /**
109: * Sets the persistence context info on this field.
110: * @param javaxPersistenceContext information on persistence context.
111: */
112: public void setJavaxPersistenceContext(
113: final JavaxPersistenceContext javaxPersistenceContext) {
114: this .javaxPersistenceContext = javaxPersistenceContext;
115: }
116:
117: /**
118: * @return true if this field is used as a persistence unit.
119: */
120: public boolean isPersistenceUnit() {
121: return javaxPersistenceUnit != null;
122: }
123:
124: /**
125: * @return the persistence unit infos.
126: */
127: public JavaxPersistenceUnit getJavaxPersistenceUnit() {
128: return javaxPersistenceUnit;
129: }
130:
131: /**
132: * Sets the persistence unit info on this field.
133: * @param javaxPersistenceUnit information on persistence unit.
134: */
135: public void setJavaxPersistenceUnit(
136: final JavaxPersistenceUnit javaxPersistenceUnit) {
137: this .javaxPersistenceUnit = javaxPersistenceUnit;
138: }
139:
140: /**
141: * @return string representation
142: */
143: @Override
144: public String toString() {
145: StringBuilder sb = new StringBuilder();
146: String titleIndent = " ";
147: // classname
148: sb.append(titleIndent);
149: sb.append(this .getClass().getName().substring(
150: this .getClass().getPackage().getName().length() + 1));
151: sb.append("[\n");
152:
153: // jEjbEJB
154: concatStringBuilder("jEjbEJB", jEjbEJB, sb);
155:
156: // jAnnotationResource
157: concatStringBuilder("jAnnotationResource", jAnnotationResource,
158: sb);
159:
160: // javaxPersistenceContext
161: concatStringBuilder("javaxPersistenceContext",
162: javaxPersistenceContext, sb);
163:
164: // javaxPersistenceUnit
165: concatStringBuilder("javaxPersistenceUnit",
166: javaxPersistenceUnit, sb);
167:
168: sb.append(titleIndent);
169: sb.append("]\n");
170: return sb.toString();
171: }
172:
173: /**
174: * Adds an entry to the given StringBuilder.
175: * @param name the name of the entry.
176: * @param object object to add.
177: * @param sb the string builder object on which add the given element.
178: * @param indent the indent to add at each line.
179: */
180: protected static void concatStringBuilder(final String name,
181: final Object object, final StringBuilder sb,
182: final String indent) {
183: if (object instanceof Boolean) {
184: // do not print if false.
185: if (!((Boolean) object).booleanValue()) {
186: return;
187: }
188: }
189: if (object != null) {
190: sb.append(indent);
191: sb.append(name);
192: sb.append("=");
193: // For arrays, use asList
194: if (object instanceof Object[]) {
195: sb.append(Arrays.asList((Object[]) object));
196: } else {
197: sb.append(object);
198: }
199: sb.append("\n");
200: }
201: }
202:
203: /**
204: * Adds an entry to the given StringBuilder.
205: * @param name the name of the entry.
206: * @param object object to add.
207: * @param sb the string builder object on which add the given element.
208: */
209: protected static void concatStringBuilder(final String name,
210: final Object object, final StringBuilder sb) {
211: concatStringBuilder(name, object, sb, " ");
212: }
213:
214: }
|