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.ClientEntry;
20  import org.apache.jetspeed.om.registry.ClientRegistry;
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.logging.JetspeedLogFactoryService;
25  import org.apache.jetspeed.services.logging.JetspeedLogger;
26  import org.apache.jetspeed.services.Registry;
27  import java.util.Enumeration;
28  
29  // regexp stuff
30  import org.apache.regexp.RE;
31  
32  /***
33   * Simple implementation of the ClientRegistry interface.
34   * <p>Extends BaseRegistry implementation to override object creation
35   * method and ensure Registry object is synchronized with its
36   * persistence backend by delegating actual addition/deletion of objects
37   * to the registry service.</p>
38   * <p>To avoid loops, a RegistryService implementation using this class
39   * nees to call the addLocalEntry/removeLocalEntry methods to modify
40   * the in memory state of this Registry</p>
41   *
42   * @author <a href="shesmer@raleigh.ibm.com">Stephan Hesmer</a>
43   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
44   * @version $Id: BaseClientRegistry.java,v 1.5 2004/02/23 03:08:26 jford Exp $
45   */
46  public class BaseClientRegistry extends BaseOrderedRegistry implements ClientRegistry
47  {
48      
49      /***
50       * Static initialization of the logger for this class
51       */    
52      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseClientRegistry.class.getName());    
53      
54      /***
55      @see Registry#setEntry
56      */
57      public void setEntry( RegistryEntry entry ) throws InvalidEntryException
58      {
59          // Delegate to the RegistryService to ensure correct handling of
60          // persistence if using file fragments
61  
62          try
63          {
64              Registry.addEntry(Registry.CLIENT, entry);
65          }
66          catch (RegistryException e)
67          {
68              logger.error("Exception", e);
69          }
70      }
71  
72      /***
73      @see Registry#addEntry
74      */
75      public void addEntry( RegistryEntry entry ) throws InvalidEntryException
76      {
77          // Delegate to the RegistryService to ensure correct handling of
78          // persistence if using file fragments
79  
80          try
81          {
82              Registry.addEntry(Registry.CLIENT, entry);
83          }
84          catch (RegistryException e)
85          {
86              logger.error("Exception", e);
87          }
88      }
89  
90      /***
91      @see Registry#removeEntry
92      */
93      public void removeEntry( String name )
94      {
95          // Delegate to the RegistryService to ensure correct handling of
96          // persistence if using file fragments
97  
98          Registry.removeEntry(Registry.CLIENT, name);
99      }
100 
101     /***
102     @see Registry#removeEntry
103     */
104     public void removeEntry( RegistryEntry entry )
105     {
106         // Delegate to the RegistryService to ensure correct handling of
107         // persistence if using file fragments
108 
109         if (entry != null)
110         {
111             Registry.removeEntry(Registry.CLIENT, entry.getName());
112         }
113     }
114 
115     /***
116      * Returns the client which matches the given useragent string.
117      *
118      * @param useragent     the useragent to match
119      * @return the found client or null if the user-agent does not match any
120      *  defined client
121      */
122     public ClientEntry findEntry(String useragent)
123     {
124         ClientEntry clientEntry = null;
125         Enumeration clients = getEntries();
126 
127         if ( logger.isDebugEnabled() )
128         {
129             logger.debug( "ClientRegistry: Looking for client with useragent :" + useragent );
130         }
131 
132         if (clients != null)
133         {
134             while (clients.hasMoreElements())
135             {
136                 ClientEntry client = (ClientEntry)clients.nextElement();
137                 if (client.getUseragentpattern() != null)
138                 {
139                     try
140                     {
141                         RE r = new RE(client.getUseragentpattern());
142                         r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
143 
144                         if (r.match(useragent))
145                         {
146 
147                             if ( logger.isDebugEnabled() )
148                             {
149                                 logger.debug( "ClientRegistry: " + useragent + " matches " + client.getUseragentpattern() );
150                             }
151 
152                             return client;
153                         }
154                         else
155                         {
156                             if ( logger.isDebugEnabled() )
157                             {
158                                 logger.debug( "ClientRegistry: " + useragent + " does not match " + client.getUseragentpattern() );
159                             }
160                         }
161                     }
162                     catch (org.apache.regexp.RESyntaxException e)
163                     {
164                         String message = "ClientRegistryService: UserAgentPattern not valid : " + client.getUseragentpattern() + " : " + e.getMessage();
165                         logger.error( message, e );
166                     }
167                 }
168             }
169         }
170 
171         return clientEntry;
172     }
173 
174     /***
175      * Creates a new RegistryEntry instance compatible with the current
176      * Registry instance implementation
177      *
178      * @return the newly created RegistryEntry
179      */
180     public RegistryEntry createEntry()
181     {
182         return new BaseClientEntry();
183     }
184 }