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.jetspeed.om.page.proxy;
18  
19  import java.lang.reflect.InvocationHandler;
20  import java.lang.reflect.Method;
21  import java.lang.reflect.Proxy;
22  
23  import org.apache.jetspeed.om.folder.Folder;
24  import org.apache.jetspeed.om.folder.proxy.FolderProxy;
25  import org.apache.jetspeed.om.page.Page;
26  import org.apache.jetspeed.page.document.proxy.NodeProxy;
27  import org.apache.jetspeed.portalsite.view.SiteView;
28  
29  /***
30   * This class proxies PSML Page instances to create a logical view
31   * of site content using the Dynamic Proxy pattern.
32   * 
33   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
34   * @version $Id: PageProxy.java 517121 2007-03-12 07:45:49Z ate $
35   */
36  public class PageProxy extends NodeProxy implements InvocationHandler
37  {
38      /***
39       * *_METHOD - Page method constants
40       */
41      protected static final Method GET_MENU_DEFINITIONS_METHOD = reflectMethod(Page.class, "getMenuDefinitions", null);
42  
43      /***
44       * page - proxy delegate page instance
45       */
46      private Page page;
47  
48      /***
49       * newInstance - creates a new proxy instance that implements the Page interface
50       *
51       * @param view site view owner of this proxy
52       * @param locatorName name of profile locator associated
53       *                    with the proxy delegate
54       * @param parentFolder view parent proxy folder
55       * @param page proxy delegate
56       */
57      public static Page newInstance(SiteView view, String locatorName, Folder parentFolder, Page page)
58      {
59          return (Page)Proxy.newProxyInstance(page.getClass().getClassLoader(), new Class[]{Page.class}, new PageProxy(view, locatorName, parentFolder, page));
60      }
61  
62      /***
63       * PageProxy - private constructor used by newInstance()
64       *
65       * @param view site view owner of this proxy
66       * @param locatorName name of profile locator associated
67       *                    with the proxy delegate
68       * @param parentFolder view parent proxy folder
69       * @param page proxy delegate
70       */
71      private PageProxy(SiteView view, String locatorName, Folder parentFolder, Page page)
72      {
73          super(view, locatorName, parentFolder, page.getName(), page.isHidden());
74          this.page = page;
75      }
76      
77      /***
78       * invoke - method invocation dispatch for this proxy, (defaults to
79       *          invocation of delegate unless method is implemented in this
80       *          proxy handler or should be hidden/stubbed)
81       *
82       * @param proxy instance invoked against
83       * @param method Page interface method invoked
84       * @param args method arguments
85       * @throws Throwable
86       */
87      public Object invoke(Object proxy, Method m, Object [] args) throws Throwable
88      {
89          // proxy implementation method dispatch
90          if (m.equals(GET_MENU_DEFINITIONS_METHOD))
91          {
92              return getMenuDefinitions();
93          }
94          else if (m.equals(GET_PARENT_METHOD))
95          {
96              return getParent();
97          }
98          else if (m.equals(GET_PATH_METHOD))
99          {
100             return getPath();
101         }
102         else if (m.equals(GET_URL_METHOD))
103         {
104             return getUrl();
105         }
106         else if (m.equals(EQUALS_METHOD))
107         {
108             return new Boolean(equals(args[0]));
109         }
110         else if (m.equals(HASH_CODE_METHOD))
111         {
112             return new Integer(hashCode());
113         }
114         else if (m.equals(IS_HIDDEN_METHOD))
115         {
116             return new Boolean(isHidden());
117         }
118         else if (m.equals(TO_STRING_METHOD))
119         {
120             return toString();
121         }
122     
123         // proxy suppression of not implemented or mutable methods
124         if (m.getName().startsWith("set"))
125         {
126             throw new RuntimeException("Page instance is immutable from proxy.");
127         }
128 
129         // attempt to invoke method on delegate Page instance
130         return m.invoke(page, args);
131     }
132 
133     /***
134      * getPage - get proxy delegate page instance
135      *
136      * @return delegate page
137      */
138     public Page getPage()
139     {
140         return page;
141     }
142 
143     /***
144      * aggregateMenuDefinitionLocators - aggregate all menu definition locators
145      *                                   in site view for this folder or page
146      */
147     protected void aggregateMenuDefinitionLocators()
148     {
149         // merge page and parent folder menu definition locators
150         // by name, (most specific page definitions are merged first
151         // since they override any folder definitions); note parent
152         // folder menu definitions include standard menu definition
153         // locator defaults
154         mergeMenuDefinitionLocators(page.getMenuDefinitions(), page);
155         FolderProxy parentFolderProxy = (FolderProxy)Proxy.getInvocationHandler(getParent());
156         mergeMenuDefinitionLocators(parentFolderProxy.getMenuDefinitionLocators());
157     }
158 }