KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > BasicMetaObject


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.metainfo.lib;
25
26 import org.objectweb.jorm.metainfo.api.MetaObject;
27 import org.objectweb.jorm.metainfo.api.Manager;
28 import org.objectweb.jorm.util.api.Loggable;
29 import org.objectweb.util.monolog.api.Logger;
30 import org.objectweb.util.monolog.api.LoggerFactory;
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * Description
38  */

39 public class BasicMetaObject implements MetaObject, Loggable {
40
41     /**
42      * The parent of the current object.
43      */

44     protected MetaObject parent;
45
46     transient protected Logger logger = null;
47     transient protected LoggerFactory loggerFactory = null;
48     transient protected boolean debug = false;
49
50     /**
51      * Builds a new BasicMetaObject object.
52      *
53      * @param parent the parent of the current object
54      */

55     public BasicMetaObject(MetaObject parent) {
56         this.parent = parent;
57         if (parent instanceof Loggable) {
58             loggerFactory = ((Loggable) parent).getLoggerFactory();
59             if (loggerFactory == null) {
60                 logger = ((Loggable) parent).getLogger();
61             } else {
62                 logger = loggerFactory.getLogger(getClass().getName());
63             }
64         }
65         debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
66     }
67
68     protected Collection JavaDoc getChildren() {
69         return null;
70     }
71
72     public void setLoggingOnChild(MetaObject mo) {
73         if (mo instanceof Loggable) {
74             if (loggerFactory != null) {
75                 ((Loggable) mo).setLoggerFactory(loggerFactory);
76             } else if (logger != null) {
77                 ((Loggable) mo).setLogger(logger);
78             }
79         }
80     }
81
82     public Logger getLogger() {
83         return logger;
84     }
85
86     public LoggerFactory getLoggerFactory() {
87         return loggerFactory;
88     }
89
90     public void setLogger(Logger logger) {
91         this.logger = logger;
92         Collection JavaDoc children = getChildren();
93         if (children != null && !children.isEmpty()) {
94             for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
95                 Object JavaDoc o = it.next();
96                 if (o != null && o instanceof Loggable) {
97                     ((Loggable) o).setLogger(logger);
98                 }
99             }
100         }
101     }
102
103     public void setLoggerFactory(LoggerFactory loggerfactory) {
104         this.loggerFactory = loggerfactory;
105         Collection JavaDoc children = getChildren();
106         if (children != null && !children.isEmpty()) {
107             for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
108                 Object JavaDoc o = it.next();
109                 if (o != null && o instanceof Loggable) {
110                     ((Loggable) o).setLoggerFactory(loggerFactory);
111                 }
112             }
113         }
114         if (loggerFactory != null && logger == null) {
115             setLogger(loggerFactory.getLogger(getClass().getName()));
116         }
117     }
118
119     ///////////////////////////////////////////////////////////////////
120
// from MetaObject interface
121
///////////////////////////////////////////////////////////////////
122

123     /**
124      * Returns the parent MetaObject of the current MetaObject.
125      *
126      * @return the MetaObject corresponding to the parent of the
127      * current object. If there is no parent, null is returned.
128      */

129     public MetaObject getParent() {
130         return parent;
131     }
132
133     /**
134      * Sets the parent of the current meta object if it is not yet done
135      * by the constructor of the meta object
136      * @param parent the parent MetaObject of the current object
137      */

138     public void setParent(MetaObject parent) {
139         this.parent = parent;
140     }
141
142     /**
143      * Returns the Manager object.
144      * @return the Manager object.
145      */

146     public Manager getManager() {
147         MetaObject tmp = parent;
148         MetaObject manager = null;
149         while (tmp != null) {
150             manager = tmp;
151             tmp = tmp.getParent();
152         }
153         return (Manager) manager;
154     }
155 }
Popular Tags