KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > ApplicationAwareMap


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.util;
27
28 import org.snipsnap.app.Application;
29 import org.radeox.util.logging.Logger;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * Wrapper for a Map which knows about different applications
37  *
38  * @author Stephan J. Schmidt
39  * @version $Id: ApplicationAwareMap.java 1606 2004-05-17 10:56:18Z leo $
40  */

41
42 public class ApplicationAwareMap {
43   protected Map JavaDoc applicationMaps;
44   private Class JavaDoc mapType;
45
46   public ApplicationAwareMap() {
47   }
48
49   public ApplicationAwareMap(Class JavaDoc outerMapType) {
50     try {
51       applicationMaps = (Map JavaDoc) outerMapType.newInstance();
52     } catch (Exception JavaDoc e) {
53       applicationMaps = new HashMap JavaDoc();
54     }
55   }
56
57   // @TODO Replace with generics
58
public ApplicationAwareMap(Class JavaDoc outerMapType, Class JavaDoc mapType) {
59     this(outerMapType);
60     this.mapType = mapType;
61   }
62
63   protected Object JavaDoc newInstance() {
64     Object JavaDoc map = null;
65     try {
66       map = mapType.newInstance();
67     } catch (Exception JavaDoc e) {
68       // This should not happen
69
System.err.println("Cannot instantiate " + mapType.getName() + " " + e);
70     }
71     return map;
72   }
73
74   public Object JavaDoc getObject(String JavaDoc applicationOid) {
75     Object JavaDoc map = applicationMaps.get(applicationOid);
76     if(null == map) {
77       map = newInstance();
78       applicationMaps.put(applicationOid, map);
79     }
80     return map;
81   }
82
83   public Queue getQueue() {
84     return (Queue) getObject();
85   }
86
87   public Map JavaDoc getMap() {
88     return (Map JavaDoc) getObject();
89   }
90
91   public Map JavaDoc getMap(String JavaDoc applicationOid) {
92     return (Map JavaDoc) getObject(applicationOid);
93   }
94
95   public Object JavaDoc getObject() {
96     String JavaDoc application = (String JavaDoc) Application.get().getObject(Application.OID);
97     return getObject(application);
98   }
99
100   /**
101    * Returns the first map containing the object.
102    * @param object
103    * @return
104    */

105   public Map JavaDoc findMap(Object JavaDoc object) {
106     //Logger.debug("searching: ["+object+"] "+applicationMaps);
107
Iterator JavaDoc mapIt = applicationMaps.values().iterator();
108     while (mapIt.hasNext()) {
109       Map JavaDoc map = (Map JavaDoc) mapIt.next();
110       if (map.containsKey(object)) {
111         return map;
112       }
113     }
114     return null;
115   }
116 }
117
Popular Tags