View Javadoc

1   /*
2    * $Id: RepositoryManagerImpl.java,v 1.2 2008/12/09 16:37:13 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.repository;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.HashMap;
9   import java.util.List;
10  import java.util.Map;
11  
12  import javax.faces.FacesException;
13  import javax.faces.context.FacesContext;
14  
15  import org.apache.commons.digester.Digester;
16  import org.apache.commons.digester.Rule;
17  import org.rcfaces.core.internal.RcfacesContext;
18  import org.rcfaces.core.provider.AbstractProvider;
19  import org.xml.sax.Attributes;
20  
21  /**
22   * 
23   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
24   * @version $Revision: 1.2 $ $Date: 2008/12/09 16:37:13 $
25   */
26  public class RepositoryManagerImpl extends AbstractProvider implements
27          IRepositoryManager {
28  
29      private static final String REVISION = "$Revision: 1.2 $";
30  
31      private static final String ID = "org.rcfaces.core.REPOSITORY_MANAGER";
32  
33      private static final String[] STRING_EMPTY_ARRAY = new String[0];
34  
35      private final Map repositories = new HashMap(16);
36  
37      public String getId() {
38          return ID;
39      }
40  
41      public void startup(FacesContext facesContext) {
42          super.startup(facesContext);
43  
44          RcfacesContext rcfacesContext = RcfacesContext
45                  .getInstance(facesContext);
46  
47          String families[] = listFamilies();
48          for (int i = 0; i < families.length; i++) {
49              String family = families[i];
50  
51              List l = (List) repositories.get(family);
52  
53              String r[] = (String[]) l.toArray(new String[l.size()]);
54  
55              repositories.put(family, r);
56          }
57  
58          rcfacesContext.setRepositoryManager(this);
59      }
60  
61      public String[] listFamilies() {
62          Collection c = repositories.keySet();
63  
64          return (String[]) c.toArray(new String[c.size()]);
65      }
66  
67      public String[] listRepositoryLocations(String family) {
68          String rls[] = (String[]) repositories.get(family);
69          if (rls == null) {
70              return STRING_EMPTY_ARRAY;
71          }
72  
73          return rls;
74      }
75  
76      public void configureRules(Digester digester) {
77          super.configureRules(digester);
78  
79          digester.addRule("rcfaces-config/repositories/repository", new Rule() {
80              private static final String REVISION = "$Revision: 1.2 $";
81  
82              public void begin(String namespace, String name,
83                      Attributes attributes) throws Exception {
84  
85                  super.digester.push(new RepositoryBean());
86              }
87  
88              public void end(String namespace, String name) throws Exception {
89                  RepositoryBean adapterBean = (RepositoryBean) super.digester
90                          .pop();
91  
92                  addRepositoryBean(adapterBean);
93              }
94          });
95  
96          digester.addBeanPropertySetter(
97                  "rcfaces-config/repositories/repository/location", "location");
98  
99          digester.addBeanPropertySetter(
100                 "rcfaces-config/repositories/repository/type", "type");
101     }
102 
103     protected void addRepositoryBean(RepositoryBean adapterBean) {
104         String type = adapterBean.getType();
105         String location = adapterBean.getLocation();
106 
107         if (type == null || location == null) {
108             throw new FacesException("Invalid repository parameters (type=\""
109                     + type + "\", location=\"" + location + "\")");
110         }
111 
112         List l = (List) repositories.get(type);
113         if (l == null) {
114             l = new ArrayList();
115 
116             repositories.put(type, l);
117         }
118 
119         l.add(location);
120     }
121 
122     /**
123      * 
124      * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
125      * @version $Revision: 1.2 $ $Date: 2008/12/09 16:37:13 $
126      */
127     public static class RepositoryBean {
128 
129         private static final String REVISION = "$Revision: 1.2 $";
130 
131         public String location;
132 
133         public String type;
134 
135         public final String getLocation() {
136             return location;
137         }
138 
139         public final void setLocation(String location) {
140             this.location = location;
141         }
142 
143         public final String getType() {
144             return type;
145         }
146 
147         public final void setType(String type) {
148             this.type = type;
149         }
150 
151     }
152 }