1   /*
2    * SingletonWithAspectJ - Singletons with AspectJ
3    * Copyright (C) 2007 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.aspectj.singleton;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.log4j.Logger;
25  import org.aspectj.lang.ProceedingJoinPoint;
26  import org.aspectj.lang.annotation.Around;
27  import org.aspectj.lang.annotation.Aspect;
28  import org.aspectj.lang.annotation.Pointcut;
29  
30  @Aspect
31  public class SingletonAspect {
32  
33  	private static final Logger log = Logger.getLogger("singleton");
34  	private final Map<Class<?>, Object> singletons;
35  
36  	public SingletonAspect() {
37  		this.singletons = new HashMap<Class<?>, Object>();
38  	}
39  
40  	/**
41  	 * All singletons are defined in this pointcut.
42  	 */
43  	@Pointcut("call(*.SingletonAop.new(..))")
44  	protected void singletons() {
45  	}
46  
47  	/**
48  	 * Circumvents the call to "new" if an instance of that class already exists.
49  	 */
50  	@Around("singletons()")
51  	public Object aroundSingletons(final ProceedingJoinPoint thisJoinPoint) throws Throwable {
52  		final Class<?> singletonClass = thisJoinPoint.getSignature().getDeclaringType();
53  		log.debug("Class: " + singletonClass);
54  		Object singletonObject = this.singletons.get(singletonClass);
55  		if (singletonObject == null) {
56  			singletonObject = thisJoinPoint.proceed();
57  			this.singletons.put(singletonClass, singletonObject);
58  		}
59  		log.debug("Object: " + singletonObject);
60  		return singletonObject;
61  	}
62  }