Project

General

Profile

« Previous | Next » 

Revision 07e4e710

Added by Cherian Mathew about 10 years ago

RemoteLazyLoadingTest : Creating application configuration programmatically
AbstractPersistentCollection, AbstractLazyInitializer : Copied aspect code here
CdmApplicationRemoteConfiguration : overriding datasource since it is not required in remoting
remotingApplicationContext : added autowiring config

View differences:

eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java
26 26
package org.hibernate.collection.internal;
27 27

  
28 28
import java.io.Serializable;
29
import java.lang.reflect.Field;
29 30
import java.util.ArrayList;
30 31
import java.util.Collection;
31 32
import java.util.Collections;
33
import java.util.HashMap;
32 34
import java.util.HashSet;
33 35
import java.util.Iterator;
34 36
import java.util.List;
35 37
import java.util.ListIterator;
38
import java.util.Map;
39
import java.util.Set;
40
import java.util.TreeMap;
41
import java.util.TreeSet;
36 42

  
37 43
import javax.naming.NamingException;
38 44

  
......
58 64
import org.hibernate.type.Type;
59 65
import org.jboss.logging.Logger;
60 66

  
67
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
68
import eu.etaxonomy.cdm.api.service.ICommonService;
69
import eu.etaxonomy.cdm.model.common.PersistentMultiLanguageText;
70

  
61 71
/**
62 72
 * Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
63 73
 *
......
545 555
		if ( initialized ) {
546 556
			return;
547 557
		}
548

  
549
		withTemporarySessionIfNeeded(
550
				new LazyInitializationWork<Object>() {
551
					@Override
552
					public Object doWork() {
553
						session.initializeCollection( AbstractPersistentCollection.this, writing );
554
						return null;
555
					}
556
				}
557
		);
558
		// Adding remote call
559
		remoteInitialize();
560
		
561
//		withTemporarySessionIfNeeded(
562
//				new LazyInitializationWork<Object>() {
563
//					@Override
564
//					public Object doWork() {
565
//						session.initializeCollection( AbstractPersistentCollection.this, writing );
566
//						return null;
567
//					}
568
//				}
569
//		);
558 570
	}
559 571

  
560 572
	private void throwLazyInitializationExceptionIfNotConnected() {
......
1171 1183
		this.owner = owner;
1172 1184
	}
1173 1185

  
1186
	/** Below is section of code which makes remote service calls */
1187
	
1188
	private static ICdmApplicationConfiguration configuration;
1189

  
1190
	
1191
	public static void setConfiguration(ICdmApplicationConfiguration conf) {
1192
		configuration = conf;
1193
	}
1194
	
1195
	private void remoteInitialize() {
1196
		
1197
		if (getOwner() != null && !wasInitialized()) {			
1198
			
1199
			try {
1200
				String role = getRole();
1201
				String fieldName = role.substring(role.lastIndexOf(".") + 1);
1202
				log.info("--> Remote Lazy Initializing " + getRole() + " , field : " + fieldName);
1203
				Object owner = getOwner();
1204
				
1205
				if(configuration == null) {
1206
					throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)");
1207
				}
1208
				ICommonService commonService = configuration.getCommonService();
1209
				if(commonService == null) {
1210
					throw new HibernateException("commonService not initialized (null)");
1211
				}
1212
				
1213
				PersistentCollection col = commonService.initializeCollection(this); 
1214
				afterInitialize();
1215

  
1216
				Class<?> clazz = getClass();
1217
				if (clazz != null) {	
1218
					CollectionField cf = getCollectionField(col);
1219
					Field field = clazz.getDeclaredField(cf.getFieldName());
1220
					field.setAccessible(true);
1221
					field.set(this, cf.getCollection());			       
1222
				}		
1223
			} catch (Exception ex) {
1224
				log.warn(ex.getMessage());
1225
			} 			
1226
		}
1227
	}
1228
	
1229
	private CollectionField getCollectionField(PersistentCollection pc) {
1230
		if(pc != null) {
1231
			if(pc instanceof PersistentSet) {
1232
				return new CollectionField(new HashSet((Set)pc), "set");
1233
			}
1234
			if(pc instanceof PersistentSortedSet) {
1235
				return new CollectionField(new TreeSet((Set)pc), "set");
1236
			}
1237
			if(pc instanceof PersistentList) {
1238
				return new CollectionField(new ArrayList((List)pc), "list");
1239
			}
1240
			if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
1241
				return new CollectionField(new HashMap((Map)pc), "map");
1242
			}
1243
			if(pc instanceof PersistentSortedMap) {
1244
				return new CollectionField(new TreeMap((Map)pc), "map");
1245
			}
1246
		}
1247
		return null;
1248
	}
1249
	
1250
	private String getCollectionFieldName(PersistentCollection pc) {
1251
		if(pc != null) {
1252
			if(pc instanceof PersistentSet || pc instanceof PersistentSortedSet) {
1253
				return "set";
1254
			}			
1255
			if(pc instanceof PersistentList) {
1256
				return "list";
1257
			}
1258
			if(pc instanceof PersistentMap || pc instanceof PersistentMultiLanguageText) {
1259
				return "map";
1260
			}
1261
		}
1262
		return null;
1263
	}
1264
	
1265
	private class CollectionField {
1266
		private Object col;
1267
		private String fieldName;
1268
		public CollectionField(Object col, String fieldName) {
1269
			this.col = col;
1270
			this.fieldName = fieldName;
1271
		}
1272
		
1273
		public Object getCollection() {
1274
			return this.col;
1275
		}
1276
		
1277
		public String getFieldName() {
1278
			return this.fieldName;
1279
		}
1280
	}
1281
	
1174 1282
}
1175 1283

  

Also available in: Unified diff