KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > models > ReflectionModel


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ReflectionModel.java,v 1.2 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.models;
21
22 /** provide a model to retrieve data by using the reflection API.
23 * for each key, a method get<key> is called without parameters, the return value is passed through*/

24
25 import java.lang.reflect.*;
26 import org.enhydra.barracuda.core.comp.*;
27 import org.enhydra.barracuda.contrib.sam.data.*;
28 import org.apache.log4j.*;
29
30 public class ReflectionModel extends AbstractTemplateModel {
31
32     protected static Logger logger = Logger.getLogger(ReflectionModel.class);
33     protected String JavaDoc name;
34     protected Object JavaDoc obj;
35
36     public ReflectionModel(String JavaDoc name) {
37         this.name = name;
38     }
39
40     public ReflectionModel(String JavaDoc name, Object JavaDoc obj) {
41         this.name = name;
42         setObject(obj);
43     }
44
45     public void setObject(Object JavaDoc obj) {
46         this.obj = obj;
47     }
48
49     public String JavaDoc getName() {
50         return name;
51     }
52
53     public Object JavaDoc getItem(String JavaDoc key) {
54         try {
55             Method m = obj.getClass().getMethod("get" + key, null);
56             return m.invoke(obj, null);
57
58         } catch (Exception JavaDoc ex) {
59             logger.error(ex.getMessage() );
60             return ex.getMessage();
61         }
62     }
63 }
64
Popular Tags