01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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"
13: * BASIS, 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.components.portletregistry;
18:
19: import java.lang.reflect.InvocationHandler;
20: import java.lang.reflect.InvocationTargetException;
21: import java.lang.reflect.Method;
22: import java.lang.reflect.Proxy;
23:
24: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
25:
26: public class MutablePortletApplicationProxy implements
27: InvocationHandler, PortletApplicationProxy {
28: private MutablePortletApplication app = null;
29: private static PortletRegistry registry;
30: private String name;
31:
32: public MutablePortletApplicationProxy(MutablePortletApplication app) {
33: this .app = app;
34: this .name = app.getName();
35: }
36:
37: public static void setRegistry(PortletRegistry r) {
38: registry = r;
39: }
40:
41: public static MutablePortletApplication createProxy(
42: MutablePortletApplication app) {
43: Class[] proxyInterfaces = new Class[] {
44: MutablePortletApplication.class,
45: PortletApplicationProxy.class };
46: MutablePortletApplication proxy = (MutablePortletApplication) Proxy
47: .newProxyInstance(MutablePortletApplication.class
48: .getClassLoader(), proxyInterfaces,
49: new MutablePortletApplicationProxy(app));
50: return proxy;
51: }
52:
53: protected void invalidate() {
54: this .app = null;
55: }
56:
57: public void setRealApplication(MutablePortletApplication app) {
58: this .app = app;
59: }
60:
61: public MutablePortletApplication getRealApplication() {
62: return app;
63: }
64:
65: public Object invoke(Object proxy, Method m, Object[] args)
66: throws Throwable {
67: try {
68: if (m.getName().equals("getRealApplication")) {
69: return getRealApplication();
70: } else if (m.getName().equals("setRealApplication")) {
71: setRealApplication((MutablePortletApplication) args[0]);
72: return null;
73: } else {
74: if (app == null) {
75: app = registry.getPortletApplication(name);
76: }
77: return m.invoke(app, args);
78: }
79: } catch (InvocationTargetException e) {
80: throw e.getTargetException();
81: }
82: }
83:
84: }
|