KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > message > codec > CodecManagerImpl


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.message.codec;
26
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.adl.Reconfiguration;
31 import org.objectweb.dream.adl.ReconfigurationFactory;
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.api.Component;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.ContentController;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38 import org.objectweb.fractal.util.Fractal;
39
40 /**
41  * Basic implementation of codec manager. Use a {@link CodecRepository }to
42  * associate codec name with ADL.
43  */

44 public class CodecManagerImpl extends AbstractComponent implements CodecManager
45 {
46
47   /**
48    * The name of the client interface bound to the component interface of the
49    * composite containning codecs.
50    */

51   public static final String JavaDoc CODECS_COMPONENT_ITF_NAME = "codecs-component";
52
53   protected CodecRepository codecRepositoryItf;
54   protected Component codecsCompositeItf;
55   protected ContentController codecsCCItf;
56
57   /**
58    * @see CodecManager#addCodec(String, Map)
59    */

60   public void addCodec(String JavaDoc codecName, Map JavaDoc context)
61       throws CodecManagerException
62   {
63     Component codec = findCodec(codecName);
64     if (codec != null)
65     {
66       return;
67     }
68     String JavaDoc codecADL = codecRepositoryItf.getCodecADL(codecName);
69     if (codecADL == null)
70     {
71       throw new CodecManagerException(codecName, "Unknown codec.");
72     }
73     createCodec(codecName, codecADL, context);
74   }
75
76   /**
77    * @see CodecManager#removeCodec(String)
78    */

79   public void removeCodec(String JavaDoc codecName) throws CodecManagerException
80   {
81     // NOT IMPLEMENTED Auto-generated method stub
82

83   }
84
85   /**
86    * @see CodecManager#getCodec(String)
87    */

88   public Component getCodec(String JavaDoc codecName) throws CodecManagerException
89   {
90     Component codec = findCodec(codecName);
91     if (codec == null)
92     {
93       throw new CodecManagerException(codecName, "Unknown codec");
94     }
95     return codec;
96   }
97
98   // ---------------------------------------------------------------------------
99
// Utility method
100
// ---------------------------------------------------------------------------
101

102   /**
103    * Finds and returns a codec component with the specified name. If the codec
104    * can't be found, returns <code>null</code>.
105    */

106   protected Component findCodec(String JavaDoc codecName)
107   {
108     Component[] codecs = codecsCCItf.getFcSubComponents();
109     for (int i = 0; i < codecs.length; i++)
110     {
111       Component component = codecs[i];
112       String JavaDoc name;
113       try
114       {
115         name = Fractal.getNameController(component).getFcName();
116       }
117       catch (NoSuchInterfaceException e)
118       {
119         break;
120       }
121       if (codecName.equals(name))
122       {
123         return component;
124       }
125     }
126     return null;
127   }
128
129   /**
130    * Creates a codec and adds it in the codecs composite
131    */

132   protected void createCodec(String JavaDoc codecName, String JavaDoc codecADL, Map JavaDoc context)
133       throws CodecManagerException
134   {
135     context.put("codecName", codecName);
136     try
137     {
138       Reconfiguration reconf = ReconfigurationFactory.getReconfiguration();
139       reconf.addComponents(codecsCompositeItf, codecADL, context);
140     }
141     catch (ADLException e)
142     {
143       throw new CodecManagerException(codecName,
144           "Unable to create codec caused by :" + e.toString());
145     }
146   }
147
148   // ---------------------------------------------------------------------------
149
// Implementation of BindingController interface
150
// ---------------------------------------------------------------------------
151

152   /**
153    * @see org.objectweb.fractal.api.control.BindingController#listFc()
154    */

155   public String JavaDoc[] listFc()
156   {
157     return new String JavaDoc[]{CodecRepository.ITF_NAME, CODECS_COMPONENT_ITF_NAME};
158   }
159
160   /**
161    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
162    * Object)
163    */

164   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
165       throws NoSuchInterfaceException, IllegalBindingException,
166       IllegalLifeCycleException
167   {
168     if (clientItfName.equals(CodecRepository.ITF_NAME))
169     {
170       codecRepositoryItf = (CodecRepository) serverItf;
171     }
172     else if (clientItfName.equals(CODECS_COMPONENT_ITF_NAME))
173     {
174       codecsCompositeItf = (Component) serverItf;
175       try
176       {
177         codecsCCItf = Fractal.getContentController(codecsCompositeItf);
178       }
179       catch (NoSuchInterfaceException e)
180       {
181         throw new IllegalBindingException("The " + CODECS_COMPONENT_ITF_NAME
182             + " client interface must be bound to a composite component");
183       }
184     }
185     super.bindFc(clientItfName, serverItf);
186   }
187 }
Popular Tags