View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.struts.config;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Comparator;
22  
23  import org.apache.commons.digester.Digester;
24  
25  public class PortletURLTypes extends AbstractConfigComponent
26  {
27      private static final Comparator portletURLTypeComparator =
28          new Comparator()
29          {
30          	public int compare(Object o1, Object o2)
31          	{
32          	    PortletURLType a1 = (PortletURLType)o1;
33          	    PortletURLType a2 = (PortletURLType)o2;
34          	    int lendiff = a1.getPath().length() - a2.getPath().length();
35          	    if ( lendiff == 0 )
36          	    {
37          	        return a1.getPath().compareTo(a2.getPath());
38          	    }
39          	    else if ( lendiff > 0 )
40          	    {
41          	        return -1;
42          	    }
43          	    else
44          	    {
45          	        return 1;
46          	    }
47          	}
48          };
49          
50      public static class URLType
51      {
52          public static final URLType ACTION = new URLType(0,"action");
53          public static final URLType RENDER = new URLType(1,"render");
54          public static final URLType RESOURCE = new URLType(2,"resource");
55          
56          private int id;
57          private String name;
58          
59          private URLType(int id, String name)
60          {
61              this.id = id;
62              this.name = name;
63          }
64          
65          public String getName()
66          {
67              return name;
68          }
69          
70          public boolean equals(URLType type)
71          {
72              return type != null ? type.id == id : false;
73          }
74      }
75              
76      public static class PortletURLType
77      {
78          private String path;
79          private URLType type;
80  
81          public PortletURLType(){}
82          public String getPath()
83          {
84              return path;
85          }
86          
87          public void setPath(String path)
88          {
89              this.path = path;
90          }
91  
92          public void setType(URLType type)
93          {
94              this.type = type;
95          }
96  
97          public URLType getType()
98          {
99              return type;
100         }
101         
102         public String toString()
103         {
104             return "PortletURLType: path="+path+", type="+type;
105         }
106     }
107     
108     private URLType defaultPortletURLType = URLType.RENDER;
109     private PortletURLType[] portletURLTypes = new PortletURLType[0];
110     private ArrayList portletURLTypeList;
111     
112     public PortletURLTypes(){}
113     
114     public void addActionType(PortletURLType portletURLType)
115     {
116         checkLoaded();
117         portletURLType.setType(URLType.ACTION);
118         portletURLTypeList.add(portletURLType);
119     }
120     
121     public void addRenderType(PortletURLType portletURLType)
122     {
123         checkLoaded();
124         portletURLType.setType(URLType.RENDER);
125         portletURLTypeList.add(portletURLType);
126     }
127     
128     public void addResourceType(PortletURLType portletURLType)
129     {
130         checkLoaded();
131         portletURLType.setType(URLType.RESOURCE);
132         portletURLTypeList.add(portletURLType);
133     }
134     
135     public void setDefault(String value)
136     {
137         checkLoaded();
138         this.defaultPortletURLType = "action".equals(value.toLowerCase()) ? URLType.ACTION : URLType.RENDER;
139     }
140     
141     public void configure(Digester digester)
142     {
143         portletURLTypeList = new ArrayList();        
144         digester.addRule("config/portlet-url-type", new SetParentRule(this));
145         digester.addSetProperties("config/portlet-url-type");
146         digester.addObjectCreate("config/portlet-url-type/action", PortletURLType.class);
147         digester.addSetProperties("config/portlet-url-type/action");
148         digester.addSetNext("config/portlet-url-type/action", "addActionType");
149         digester.addObjectCreate("config/portlet-url-type/render", PortletURLType.class);
150         digester.addSetProperties("config/portlet-url-type/render");
151         digester.addSetNext("config/portlet-url-type/render", "addRenderType");
152         digester.addObjectCreate("config/portlet-url-type/resource", PortletURLType.class);
153         digester.addSetProperties("config/portlet-url-type/resource");
154         digester.addSetNext("config/portlet-url-type/resource", "addResourceType");
155         digester.addCallMethod("config/portlet-url-type", "afterLoad");
156     }
157     
158     public void afterLoad()
159     {
160         super.afterLoad();
161 
162         if ( portletURLTypeList != null && portletURLTypeList.size() > 0 )
163         {
164             portletURLTypes = new PortletURLType[portletURLTypeList.size()];
165             for ( int i = 0; i < portletURLTypes.length; i++ )
166             {
167                 portletURLTypes[i] = (PortletURLType)portletURLTypeList.get(i);
168             }
169             if ( portletURLTypes.length > 1 )
170             {
171                 Arrays.sort(portletURLTypes, portletURLTypeComparator);
172             }
173         }
174 
175         portletURLTypeList = null;
176     }
177     
178     public URLType getType(String path)
179     {
180         URLType type = defaultPortletURLType;
181         for (int i = 0; i < portletURLTypes.length; i++ )
182         {
183             if (path.startsWith(portletURLTypes[i].path))
184             {
185                 type = portletURLTypes[i].getType();
186                 break;
187             }
188         }
189         return type;
190     }
191 }