View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.logic.interceptor;
17  
18  import java.lang.reflect.Method;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.aopalliance.intercept.MethodInvocation;
23  import org.apache.axis.components.logger.LogFactory;
24  import org.apache.commons.logging.Log;
25  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
26  import org.seasar.tuigwaa.logic.aop.MethodInvoker;
27  import org.seasar.tuigwaa.logic.aop.MethodRegister;
28  import org.seasar.tuigwaa.logic.aop.MethodSignature;
29  import org.seasar.tuigwaa.model.DataService;
30  import org.seasar.tuigwaa.model.ModelService;
31  import org.seasar.tuigwaa.model.core.TgwEntity;
32  
33  public class DataServiceMethodRegisterImpl implements MethodRegister,
34  		DataServiceMethodRegister {
35  
36  	private Log log = LogFactory.getLog(DataServiceMethodRegisterImpl.class.getName());
37  	
38  	private ModelService model = (ModelService) SingletonS2ContainerFactory
39  			.getContainer().getComponent(ModelService.class);
40  
41  	private Map delegateMethods = new HashMap();
42  
43  	public DataServiceMethodRegisterImpl() {
44  	}
45  
46  	public void registDelegateObject(String siteName, String entityName,
47  			DataService dataService) {
48  		Method[] methods = dataService.getClass().getDeclaredMethods();
49  
50  		for (int i = 0; i < methods.length; i++) {
51  			MethodSignature msig = new MethodSignature(methods[i]);
52  			TgwEntity entity = model.getEntityByDisplayName(siteName,
53  					entityName);
54  			if(entity == null){
55  				entity = model.getEntity(siteName, entityName);
56  			}
57  			if(entity != null){
58  						DataMethodSignature dsig = new DataMethodSignature(entity, msig);
59  						delegateMethods.put(dsig,
60  					new MethodInvoker(methods[i], dataService));
61  			}else{
62  				log.warn("Not found entity. " + entityName);
63  			}
64  		}
65  	}
66  
67  	public MethodInvoker[] getBeforeMethodInvokers(MethodInvocation invocation) {
68  		return null;
69  	}
70  
71  	public MethodInvoker getDelegateMethodInvoker(MethodInvocation invocation) {
72  		MethodSignature sig = new MethodSignature(invocation.getMethod());
73  
74  		Object obj = invocation.getArguments()[0];
75  		if (!(obj instanceof TgwEntity)) {
76  			return null;
77  		}
78  		TgwEntity entity = (TgwEntity) invocation.getArguments()[0];
79  		DataMethodSignature datasig = new DataMethodSignature(entity, sig);
80  		return (MethodInvoker) delegateMethods.get(datasig);
81  	}
82  
83  	public MethodInvoker[] getAfterMethodInvokers(MethodInvocation invocation) {
84  		return null;
85  	}
86  
87  	public class DataMethodSignature {
88  
89  		private volatile int hashCode = 0;
90  
91  		private TgwEntity entity;
92  
93  		private MethodSignature signature;
94  
95  		public DataMethodSignature(TgwEntity entity, MethodSignature signature) {
96  			this.entity = entity;
97  			this.signature = signature;
98  		}
99  
100 		public TgwEntity getEntity() {
101 			return entity;
102 		}
103 
104 		public MethodSignature getSignature() {
105 			return signature;
106 		}
107 
108 		public boolean equals(Object obj) {
109 			if (obj == null || !(obj instanceof DataMethodSignature)) {
110 				return false;
111 			}
112 			DataMethodSignature sig = (DataMethodSignature) obj;
113 			if (entity == null) {
114 				if (sig.getEntity() != null) {
115 					return false;
116 				} else if (!signature.equals(sig.getSignature())) {
117 					return false;
118 				} else {
119 					return true;
120 				}
121 			}
122 			return entity.equals(sig.getEntity())
123 					&& signature.equals(sig.getSignature());
124 		}
125 
126 		public int hashCode() {
127 			int seed = 17;
128 			int weight = 37;
129 
130 			if (this.hashCode == 0) {
131 				this.hashCode = seed;
132 				if (entity != null) {
133 					this.hashCode += weight * this.getEntity().hashCode();
134 				}
135 				this.hashCode += weight * this.getSignature().hashCode();
136 			}
137 			return this.hashCode;
138 		}
139 	}
140 }