View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.om.registry.base;
18  
19  import org.apache.jetspeed.om.registry.MediaTypeRegistry;
20  import org.apache.jetspeed.om.registry.MediaTypeEntry;
21  import org.apache.jetspeed.om.registry.RegistryEntry;
22  import org.apache.jetspeed.om.registry.InvalidEntryException;
23  import org.apache.jetspeed.om.registry.RegistryException;
24  import org.apache.jetspeed.services.Registry;
25  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
26  import org.apache.jetspeed.services.logging.JetspeedLogger;
27  import org.apache.jetspeed.capability.CapabilityMap;
28  
29  import java.util.Iterator;
30  import java.util.Enumeration;
31  import java.util.List;
32  import java.util.ArrayList;
33  
34  /***
35   * Extends BaseRegistry implementation to override object creation
36   * method
37   *
38   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
39   * @version $Id: BaseMediaTypeRegistry.java,v 1.7 2004/02/23 03:08:26 jford Exp $
40   */
41  public class BaseMediaTypeRegistry extends BaseOrderedRegistry
42          implements MediaTypeRegistry
43  {
44  
45      /***
46       * Static initialization of the logger for this class
47       */    
48      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseMediaTypeRegistry.class.getName());    
49      
50      /*
51       * Find media-types in this registry that match the CapabilityMap requirements
52       *
53       * @param category The category and optional subcategories.
54       * @return Iterator The result as an iterator.
55       */
56      public Iterator findForCapability(CapabilityMap cm)
57      {
58          if (cm == null)
59          {
60              return null;
61          }
62  
63          String type = cm.getPreferredType().getContentType();
64          List result = new ArrayList();
65  
66          if ( logger.isDebugEnabled() )
67          {
68              logger.debug("MediaTypeRegistry: looking for type " + type );
69          }
70  
71          if (type == null)
72          {
73              return result.iterator();
74          }
75  
76          try
77          {
78              Enumeration en = getEntries();
79              while(en.hasMoreElements())
80              {
81                  MediaTypeEntry mte = (MediaTypeEntry)en.nextElement();
82  
83                  if ( logger.isDebugEnabled() )
84                  {
85                      logger.debug("MediaTypeRegistry: found MediaTypeEntry for type " + mte.getMimeType() );
86                  }
87                  if (type.equals(mte.getMimeType()))
88                  {
89                      result.add(mte);
90                  }
91              }
92          }
93          catch(Exception e)
94          {
95              logger.error("Exception", e);
96          }
97  
98          if ( logger.isDebugEnabled() )
99          {
100             logger.debug("MediaTypeRegistry: found " + result.size() + " entries." );
101         }
102 
103         return result.iterator();
104 
105     }
106 
107     /***
108     @see Registry#setEntry
109     */
110     public void setEntry( RegistryEntry entry ) throws InvalidEntryException
111     {
112         // Delegate to the RegistryService to ensure correct handling of
113         // persistence if using file fragments
114 
115         try
116         {
117             Registry.addEntry(Registry.MEDIA_TYPE, entry);
118         }
119         catch (RegistryException e)
120         {
121             logger.error("Exception", e);
122         }
123     }
124 
125     /***
126     @see Registry#addEntry
127     */
128     public void addEntry( RegistryEntry entry ) throws InvalidEntryException
129     {
130         // Delegate to the RegistryService to ensure correct handling of
131         // persistence if using file fragments
132 
133         try
134         {
135             Registry.addEntry(Registry.MEDIA_TYPE, entry);
136         }
137         catch (RegistryException e)
138         {
139             logger.error("Exception", e);
140         }
141     }
142 
143     /***
144     @see Registry#removeEntry
145     */
146     public void removeEntry( String name )
147     {
148         // Delegate to the RegistryService to ensure correct handling of
149         // persistence if using file fragments
150 
151         Registry.removeEntry(Registry.MEDIA_TYPE, name);
152     }
153 
154     /***
155     @see Registry#removeEntry
156     */
157     public void removeEntry( RegistryEntry entry )
158     {
159         // Delegate to the RegistryService to ensure correct handling of
160         // persistence if using file fragments
161 
162         if (entry != null)
163         {
164             Registry.removeEntry(Registry.MEDIA_TYPE, entry.getName());
165         }
166     }
167 
168     /***
169      * Creates a new RegistryEntry instance compatible with the current
170      * Registry instance implementation
171      *
172      * @return the newly created RegistryEntry
173      */
174     public RegistryEntry createEntry()
175     {
176         return new BaseMediaTypeEntry();
177     }
178 }