KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > mapping > DefaultMappingStrategy


1 /**
2  * Copyright (C) 2002 Kelua SA
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 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
19 package org.objectweb.kilim.model.mapping;
20
21 import java.util.HashMap JavaDoc;
22
23 import org.objectweb.kilim.KilimException;
24
25 /**
26  * This class is a simple container dedicated to the definition of instanciation strategies. It is used by the component
27  * factory to find the instanciation manager to be associated to each application component. It is possible to redefine
28  * a default instanciation manager, and to define per template managers and per instance managers.
29  * The factory uses the following look up strategy. The search is stopped as soon as a manager is found.
30  * the component factory first looks for a manager explicitely associated to the component.
31  * If none can be found, it then looks for a manager explicitely associated to the component template.
32  * If none can be found, it recursively looks for a manager explicitely associated to super templates.
33  * If none can be found it looks for a default manager.
34  *
35  * No null value can be returned, since this class defines a NAryPreInstanciationMger as default instanciation manager
36  * and no null value is accepted by the setDefaultMger().
37  * @author horn
38  */

39
40 public class DefaultMappingStrategy implements MappingStrategy {
41     private Mapper defaultMapper = new JavaRuntimeMapper();
42     private HashMap JavaDoc perTemplateMapper;
43     private HashMap JavaDoc perInstanceMapper;
44     
45     /**
46      * @see java.lang.Object#Object()
47      */

48     public DefaultMappingStrategy() throws KilimException {
49     }
50     
51     /**
52      * Method DefaultMappingStrategy.
53      * @param aMapper : a default mapper (this ctor just contains a call to setDefaultMapper(aMapper)
54      * @throws KilimException : generated if the reference is null. A NullMapper should be indicated if
55      * no mapping should be performed at runtime.
56      */

57     public DefaultMappingStrategy(Mapper aMapper) throws KilimException {
58         setDefaultMapper(aMapper);
59     }
60
61     /**
62      * @see org.objectweb.kilim.model.mapping.MappingStrategy#setDefaultMapper(Mapper)
63      */

64     public void setDefaultMapper(Mapper aMapper) throws KilimException {
65         if (aMapper == null) {
66             throw new KilimException("attempt to install a null mapper as default");
67         }
68         defaultMapper = aMapper;
69     }
70     
71     /**
72      * @see org.objectweb.kilim.model.mapping.MappingStrategy#getDefaultMapper()
73      */

74     public Mapper getDefaultMapper() {
75         return defaultMapper;
76     }
77
78     /**
79      * @see org.objectweb.kilim.model.mapping.MappingStrategy#setPerTemplateMapper(String, Mapper)
80      */

81     public void setPerTemplateMapper(String JavaDoc aName, Mapper aMapper) throws KilimException {
82         if (aName == null) {
83             throw new KilimException("attempt to install a manager for template through a null name");
84         }
85
86         if (perTemplateMapper == null) {
87             perTemplateMapper = new HashMap JavaDoc();
88         }
89         perTemplateMapper.put(aName, aMapper);
90     }
91     
92     /**
93      * @see org.objectweb.kilim.model.mapping.MappingStrategy#getPerTemplateMapper(String)
94      */

95     public Mapper getPerTemplateMapper(String JavaDoc aName) throws KilimException {
96         if (aName == null) {
97             throw new KilimException("attempt to get a template manager through a null name ");
98         }
99         
100         if (perTemplateMapper == null) {
101             return null;
102         }
103         return (Mapper) perTemplateMapper.get(aName);
104     }
105
106     /**
107      * @see org.objectweb.kilim.model.mapping.MappingStrategy#setPerInstanceMapper(String, Mapper)
108      */

109     public void setPerInstanceMapper(String JavaDoc aName, Mapper aMapper) throws KilimException {
110         if (aMapper == null) {
111             throw new KilimException("attempt to install a null mapper for template " + aName);
112         }
113
114         if (perInstanceMapper == null) {
115             perInstanceMapper = new HashMap JavaDoc();
116         }
117         perInstanceMapper.put(aName, aMapper);
118     }
119     
120     /**
121      * @see org.objectweb.kilim.model.mapping.MappingStrategy#getPerInstanceMapper(String)
122      */

123     public Mapper getPerInstanceMapper(String JavaDoc aName) throws KilimException {
124         if (aName == null) {
125             throw new KilimException("attempt to get a instance manager through a null name ");
126         }
127         
128         if (perInstanceMapper == null) {
129             return null;
130         }
131         return (Mapper) perInstanceMapper.get(aName);
132     }
133 }
134
Popular Tags