KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > repository > plugins > AbstractKernelRepository


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.repository.plugins;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.jboss.repository.spi.KernelRepository;
29 import org.jboss.repository.spi.MetaDataCombiner;
30 import org.jboss.repository.spi.Key;
31 import org.jboss.repository.spi.MetaData;
32 import org.jboss.repository.spi.MetaDataLoader;
33 import org.jboss.repository.spi.CommonNames;
34
35 /**
36  @todo update for synchronization and concurrent reader maps
37
38  @author Scott.Stark@jboss.org
39  @version $Revision: 57306 $
40  */

41 public class AbstractKernelRepository
42    implements KernelRepository
43 {
44    private Map JavaDoc repository = new HashMap JavaDoc(103);
45    private Map JavaDoc domainMap = new HashMap JavaDoc(103);
46    private Map JavaDoc clusterMap = new HashMap JavaDoc(103);
47    private Map JavaDoc serverMap = new HashMap JavaDoc(103);
48    private Map JavaDoc appMap = new HashMap JavaDoc(103);
49    private Map JavaDoc deployMap = new HashMap JavaDoc(103);
50    private Map JavaDoc sessionMap = new HashMap JavaDoc(103);
51    private Map JavaDoc maps[] = {
52       domainMap,
53       clusterMap,
54       serverMap,
55       appMap,
56       deployMap,
57       sessionMap
58    };
59
60    private MetaDataCombiner combiner;
61
62    public AbstractKernelRepository(MetaDataCombiner combiner)
63       throws Exception JavaDoc
64    {
65       super();
66       this.combiner = combiner;
67    }
68
69    public Object JavaDoc getMetaData(Key key)
70    {
71       return getMetaData(key, combiner);
72    }
73
74    /**
75     @todo should the loader be used to attempt to load data if the requested
76       name does not currently exist?
77
78     @param key
79     @param combiner
80     @return the metadata object for name if found, null otherwise
81     */

82    public Object JavaDoc getMetaData(Key key, MetaDataCombiner combiner)
83    {
84       // Should the loader be used here?
85
Map JavaDoc attributes = key.getAttributes();
86       HashMap JavaDoc tmp = new HashMap JavaDoc();
87       MetaData[] levelData = new MetaData[CommonNames.N_LEVELS];
88       int level = key.getLevel();
89       for(int n = 0; n <= level; n ++)
90       {
91          String JavaDoc levelKey = CommonNames.LEVELS[n];
92          String JavaDoc value = (String JavaDoc) attributes.get(levelKey);
93          tmp.put(levelKey, value);
94          Key tmpKey = new Key(key.getName(), tmp);
95          levelData[n] = (MetaData) maps[n].get(tmpKey);
96       }
97
98       return combiner.combine(key, levelData);
99    }
100
101    /**
102     Get the MetaData for the key and all its subkeys
103     @param key
104     @return Map<Key, MetaData> for the key and matching subkeys
105     */

106    public Map JavaDoc getAllMetaData(Key key)
107    {
108        HashMap JavaDoc levelData = new HashMap JavaDoc();
109        Map JavaDoc attributes = key.getAttributes();
110        HashMap JavaDoc tmp = new HashMap JavaDoc();
111        int level = key.getLevel();
112        for(int n = 0; n <= level; n ++)
113        {
114           String JavaDoc levelKey = CommonNames.LEVELS[n];
115           String JavaDoc value = (String JavaDoc) attributes.get(levelKey);
116           tmp.put(levelKey, value);
117           Key tmpKey = new Key(key.getName(), tmp);
118           MetaData data = (MetaData) maps[n].get(tmpKey);
119           if( data != null )
120             levelData.put(tmpKey, data);
121        }
122
123        return levelData;
124    }
125
126    public synchronized MetaData addMetaData(Key key, MetaData data)
127    {
128       MetaData prev = (MetaData) repository.put(key, data);
129       // Add
130
int level = key.getLevel();
131       maps[level].put(key, data);
132       return prev;
133    }
134    public MetaData removeMetaData(Key key)
135    {
136       int level = key.getLevel();
137       MetaData prev = (MetaData) maps[level].remove(key);
138       repository.remove(key);
139       return prev;
140    }
141
142    public void loadMetaData(MetaDataLoader loader)
143    {
144       Iterator JavaDoc keys = loader.getKeys();
145       while( keys.hasNext() )
146       {
147          Key key = (Key) keys.next();
148          MetaData data = loader.load(key);
149          int level = key.getLevel();
150          maps[level].put(key, data);
151       }
152    }
153
154    public Iterator JavaDoc getKeyNames()
155    {
156       return repository.keySet().iterator();
157    }
158
159    /**
160     @todo Not implemented.
161     @param nameRE
162     @param attributes
163     @return null currently
164     */

165    public Iterator JavaDoc findKeys(String JavaDoc nameRE, Map JavaDoc attributes)
166    {
167       return null;
168    }
169
170 }
171
Popular Tags