1.1 --- a/spring.lookup/build.xml Tue Apr 21 21:41:25 2009 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,43 +0,0 @@
1.4 -<?xml version="1.0" encoding="UTF-8"?>
1.5 -<!--
1.6 -DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.7 -
1.8 -Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
1.9 -
1.10 -The contents of this file are subject to the terms of either the GNU
1.11 -General Public License Version 2 only ("GPL") or the Common
1.12 -Development and Distribution License("CDDL") (collectively, the
1.13 -"License"). You may not use this file except in compliance with the
1.14 -License. You can obtain a copy of the License at
1.15 -http://www.netbeans.org/cddl-gplv2.html
1.16 -or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.17 -specific language governing permissions and limitations under the
1.18 -License. When distributing the software, include this License Header
1.19 -Notice in each file and include the License file at
1.20 -nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.21 -particular file as subject to the "Classpath" exception as provided
1.22 -by Sun in the GPL Version 2 section of the License file that
1.23 -accompanied this code. If applicable, add the following below the
1.24 -License Header, with the fields enclosed by brackets [] replaced by
1.25 -your own identifying information:
1.26 -"Portions Copyrighted [year] [name of copyright owner]"
1.27 -
1.28 -If you wish your version of this file to be governed by only the CDDL
1.29 -or only the GPL Version 2, indicate your decision by adding
1.30 -"[Contributor] elects to include this software in this distribution
1.31 -under the [CDDL or GPL Version 2] license." If you do not indicate a
1.32 -single choice of license, a recipient has the option to distribute
1.33 -your version of this file under either the CDDL, the GPL Version 2 or
1.34 -to extend the choice of license to its licensees as provided above.
1.35 -However, if you add GPL Version 2 code and therefore, elected the GPL
1.36 -Version 2 license, then the option applies only if the new code is
1.37 -made subject to such option by the copyright holder.
1.38 -
1.39 -Contributor(s):
1.40 -
1.41 -Portions Copyrighted 2008 Sun Microsystems, Inc.
1.42 --->
1.43 -<project basedir="." default="netbeans" name="contrib/spring.lookup">
1.44 - <description>Builds, tests, and runs the project org.netbeans.modules.spring.lookup</description>
1.45 - <import file="../../nbbuild/templates/projectized.xml"/>
1.46 -</project>
2.1 --- a/spring.lookup/nbproject/project.properties Tue Apr 21 21:41:25 2009 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,42 +0,0 @@
2.4 -is.autoload=true
2.5 -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 -#
2.7 -# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2.8 -#
2.9 -# The contents of this file are subject to the terms of either the GNU
2.10 -# General Public License Version 2 only ("GPL") or the Common
2.11 -# Development and Distribution License("CDDL") (collectively, the
2.12 -# "License"). You may not use this file except in compliance with the
2.13 -# License. You can obtain a copy of the License at
2.14 -# http://www.netbeans.org/cddl-gplv2.html
2.15 -# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.16 -# specific language governing permissions and limitations under the
2.17 -# License. When distributing the software, include this License Header
2.18 -# Notice in each file and include the License file at
2.19 -# nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
2.20 -# particular file as subject to the "Classpath" exception as provided
2.21 -# by Sun in the GPL Version 2 section of the License file that
2.22 -# accompanied this code. If applicable, add the following below the
2.23 -# License Header, with the fields enclosed by brackets [] replaced by
2.24 -# your own identifying information:
2.25 -# "Portions Copyrighted [year] [name of copyright owner]"
2.26 -#
2.27 -# If you wish your version of this file to be governed by only the CDDL
2.28 -# or only the GPL Version 2, indicate your decision by adding
2.29 -# "[Contributor] elects to include this software in this distribution
2.30 -# under the [CDDL or GPL Version 2] license." If you do not indicate a
2.31 -# single choice of license, a recipient has the option to distribute
2.32 -# your version of this file under either the CDDL, the GPL Version 2 or
2.33 -# to extend the choice of license to its licensees as provided above.
2.34 -# However, if you add GPL Version 2 code and therefore, elected the GPL
2.35 -# Version 2 license, then the option applies only if the new code is
2.36 -# made subject to such option by the copyright holder.
2.37 -#
2.38 -# Contributor(s):
2.39 -#
2.40 -# Portions Copyrighted 2008 Sun Microsystems, Inc.
2.41 -javac.source=1.5
2.42 -javac.compilerargs=-Xlint -Xlint:-serial
2.43 -javadoc.apichanges=${basedir}/apichanges.xml
2.44 -
2.45 -javadoc.arch=${basedir}/arch.xml
3.1 --- a/spring.lookup/nbproject/project.xml Tue Apr 21 21:41:25 2009 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,68 +0,0 @@
3.4 -<?xml version="1.0" encoding="UTF-8"?>
3.5 -<!--
3.6 -DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.7 -
3.8 -Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
3.9 -
3.10 -The contents of this file are subject to the terms of either the GNU
3.11 -General Public License Version 2 only ("GPL") or the Common
3.12 -Development and Distribution License("CDDL") (collectively, the
3.13 -"License"). You may not use this file except in compliance with the
3.14 -License. You can obtain a copy of the License at
3.15 -http://www.netbeans.org/cddl-gplv2.html
3.16 -or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.17 -specific language governing permissions and limitations under the
3.18 -License. When distributing the software, include this License Header
3.19 -Notice in each file and include the License file at
3.20 -nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
3.21 -particular file as subject to the "Classpath" exception as provided
3.22 -by Sun in the GPL Version 2 section of the License file that
3.23 -accompanied this code. If applicable, add the following below the
3.24 -License Header, with the fields enclosed by brackets [] replaced by
3.25 -your own identifying information:
3.26 -"Portions Copyrighted [year] [name of copyright owner]"
3.27 -
3.28 -If you wish your version of this file to be governed by only the CDDL
3.29 -or only the GPL Version 2, indicate your decision by adding
3.30 -"[Contributor] elects to include this software in this distribution
3.31 -under the [CDDL or GPL Version 2] license." If you do not indicate a
3.32 -single choice of license, a recipient has the option to distribute
3.33 -your version of this file under either the CDDL, the GPL Version 2 or
3.34 -to extend the choice of license to its licensees as provided above.
3.35 -However, if you add GPL Version 2 code and therefore, elected the GPL
3.36 -Version 2 license, then the option applies only if the new code is
3.37 -made subject to such option by the copyright holder.
3.38 -
3.39 -Contributor(s):
3.40 -
3.41 -Portions Copyrighted 2008 Sun Microsystems, Inc.
3.42 --->
3.43 -<project xmlns="http://www.netbeans.org/ns/project/1">
3.44 - <type>org.netbeans.modules.apisupport.project</type>
3.45 - <configuration>
3.46 - <data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
3.47 - <code-name-base>org.netbeans.modules.spring.lookup</code-name-base>
3.48 - <module-dependencies>
3.49 - <dependency>
3.50 - <code-name-base>org.netbeans.libs.springframework</code-name-base>
3.51 - <build-prerequisite/>
3.52 - <compile-dependency/>
3.53 - <run-dependency>
3.54 - <specification-version>1.0</specification-version>
3.55 - </run-dependency>
3.56 - </dependency>
3.57 - <dependency>
3.58 - <code-name-base>org.openide.util</code-name-base>
3.59 - <build-prerequisite/>
3.60 - <compile-dependency/>
3.61 - <run-dependency>
3.62 - <specification-version>7.12</specification-version>
3.63 - </run-dependency>
3.64 - </dependency>
3.65 - </module-dependencies>
3.66 - <public-packages>
3.67 - <package>org.netbeans.api.spring</package>
3.68 - </public-packages>
3.69 - </data>
3.70 - </configuration>
3.71 -</project>
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/spring.lookup/pom.xml Tue Apr 21 22:17:32 2009 +0200
4.3 @@ -0,0 +1,71 @@
4.4 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.5 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.6 + <modelVersion>4.0.0</modelVersion>
4.7 + <groupId>org.apidesign</groupId>
4.8 + <artifactId>spring-lookup</artifactId>
4.9 + <packaging>jar</packaging>
4.10 + <version>1.0</version>
4.11 + <name>spring-lookup</name>
4.12 + <url>http://maven.apache.org</url>
4.13 + <repositories>
4.14 + <repository>
4.15 + <id>netbeans</id>
4.16 + <url>http://bits.netbeans.org/maven2/</url>
4.17 + </repository>
4.18 + </repositories>
4.19 + <build>
4.20 + <plugins>
4.21 + <plugin>
4.22 + <groupId>org.apache.maven.plugins</groupId>
4.23 + <artifactId>maven-compiler-plugin</artifactId>
4.24 + <version>2.0.2</version>
4.25 + <configuration>
4.26 + <source>1.5</source>
4.27 + <target>1.5</target>
4.28 + </configuration>
4.29 + </plugin>
4.30 + <plugin>
4.31 + <groupId>org.apache.maven.plugins</groupId>
4.32 + <artifactId>maven-javadoc-plugin</artifactId>
4.33 + <version>2.5</version>
4.34 + <configuration>
4.35 + <excludePackageNames>org.netbeans.modules.openide.util</excludePackageNames>
4.36 + </configuration>
4.37 + </plugin>
4.38 + </plugins>
4.39 + </build>
4.40 + <dependencies>
4.41 + <dependency>
4.42 + <groupId>org.apidesign</groupId>
4.43 + <artifactId>lookup</artifactId>
4.44 + <version>7.22.0</version>
4.45 + </dependency>
4.46 + <dependency>
4.47 + <groupId>org.netbeans.api</groupId>
4.48 + <artifactId>org-netbeans-modules-nbjunit</artifactId>
4.49 + <version>RELEASE65</version>
4.50 + <scope>test</scope>
4.51 + </dependency>
4.52 + <dependency>
4.53 + <groupId>org.netbeans.modules</groupId>
4.54 + <artifactId>org-netbeans-insane</artifactId>
4.55 + <version>RELEASE65</version>
4.56 + <scope>test</scope>
4.57 + </dependency>
4.58 + <dependency>
4.59 + <groupId>org.springframework</groupId>
4.60 + <artifactId>spring-core</artifactId>
4.61 + <version>2.5.6</version>
4.62 + </dependency>
4.63 + <dependency>
4.64 + <groupId>org.springframework</groupId>
4.65 + <artifactId>spring-beans</artifactId>
4.66 + <version>2.5.6</version>
4.67 + </dependency>
4.68 + <dependency>
4.69 + <groupId>org.springframework</groupId>
4.70 + <artifactId>spring-context</artifactId>
4.71 + <version>2.5.6</version>
4.72 + </dependency>
4.73 + </dependencies>
4.74 +</project>
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/spring.lookup/src/main/java/org/netbeans/api/spring/NbSpring.java Tue Apr 21 22:17:32 2009 +0200
5.3 @@ -0,0 +1,77 @@
5.4 +/*
5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 + *
5.7 + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5.8 + *
5.9 + * The contents of this file are subject to the terms of either the GNU
5.10 + * General Public License Version 2 only ("GPL") or the Common
5.11 + * Development and Distribution License("CDDL") (collectively, the
5.12 + * "License"). You may not use this file except in compliance with the
5.13 + * License. You can obtain a copy of the License at
5.14 + * http://www.netbeans.org/cddl-gplv2.html
5.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
5.16 + * specific language governing permissions and limitations under the
5.17 + * License. When distributing the software, include this License Header
5.18 + * Notice in each file and include the License file at
5.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
5.20 + * particular file as subject to the "Classpath" exception as provided
5.21 + * by Sun in the GPL Version 2 section of the License file that
5.22 + * accompanied this code. If applicable, add the following below the
5.23 + * License Header, with the fields enclosed by brackets [] replaced by
5.24 + * your own identifying information:
5.25 + * "Portions Copyrighted [year] [name of copyright owner]"
5.26 + *
5.27 + * If you wish your version of this file to be governed by only the CDDL
5.28 + * or only the GPL Version 2, indicate your decision by adding
5.29 + * "[Contributor] elects to include this software in this distribution
5.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
5.31 + * single choice of license, a recipient has the option to distribute
5.32 + * your version of this file under either the CDDL, the GPL Version 2 or
5.33 + * to extend the choice of license to its licensees as provided above.
5.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
5.35 + * Version 2 license, then the option applies only if the new code is
5.36 + * made subject to such option by the copyright holder.
5.37 + *
5.38 + * Contributor(s):
5.39 + *
5.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
5.41 + */
5.42 +
5.43 +package org.netbeans.api.spring;
5.44 +
5.45 +import org.netbeans.modules.spring.lookup.ApplicationContextBasedLookup;
5.46 +import org.netbeans.modules.spring.lookup.LookupBasedApplicationContext;
5.47 +import org.openide.util.Lookup;
5.48 +import org.springframework.context.ApplicationContext;
5.49 +
5.50 +/** Factory methods to manipulate and bridge NetBeans Lookup and SpringFramework's
5.51 + * ApplicationContext.
5.52 + * @author Jaroslav Tulach, Andrei Badea
5.53 + * @since 1.1
5.54 + */
5.55 +public class NbSpring {
5.56 + private NbSpring() {
5.57 + }
5.58 +
5.59 + /** Converts an application context to lookup.
5.60 + *
5.61 + * @param context application context
5.62 + * @return lookup that bridges to the context
5.63 + */
5.64 + public static Lookup create(ApplicationContext context) {
5.65 + return new ApplicationContextBasedLookup(context);
5.66 + }
5.67 +
5.68 + /** From lookup to application context, if necesary.
5.69 + *
5.70 + * @param context application context
5.71 + * @return lookup that bridges to the context
5.72 + */
5.73 + public static ApplicationContext create(Lookup lookup) {
5.74 + ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
5.75 + if (l == null) {
5.76 + l = NbSpring.class.getClassLoader();
5.77 + }
5.78 + return LookupBasedApplicationContext.create(lookup, l);
5.79 + }
5.80 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/spring.lookup/src/main/java/org/netbeans/modules/spring/lookup/ApplicationContextBasedLookup.java Tue Apr 21 22:17:32 2009 +0200
6.3 @@ -0,0 +1,241 @@
6.4 +/*
6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6.6 + *
6.7 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
6.8 + *
6.9 + * The contents of this file are subject to the terms of either the GNU
6.10 + * General Public License Version 2 only ("GPL") or the Common
6.11 + * Development and Distribution License("CDDL") (collectively, the
6.12 + * "License"). You may not use this file except in compliance with the
6.13 + * License. You can obtain a copy of the License at
6.14 + * http://www.netbeans.org/cddl-gplv2.html
6.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
6.16 + * specific language governing permissions and limitations under the
6.17 + * License. When distributing the software, include this License Header
6.18 + * Notice in each file and include the License file at
6.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
6.20 + * particular file as subject to the "Classpath" exception as provided
6.21 + * by Sun in the GPL Version 2 section of the License file that
6.22 + * accompanied this code. If applicable, add the following below the
6.23 + * License Header, with the fields enclosed by brackets [] replaced by
6.24 + * your own identifying information:
6.25 + * "Portions Copyrighted [year] [name of copyright owner]"
6.26 + *
6.27 + * Contributor(s):
6.28 + *
6.29 + * The Original Software is NetBeans. The Initial Developer of the Original
6.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
6.31 + * Microsystems, Inc. All Rights Reserved.
6.32 + *
6.33 + * If you wish your version of this file to be governed by only the CDDL
6.34 + * or only the GPL Version 2, indicate your decision by adding
6.35 + * "[Contributor] elects to include this software in this distribution
6.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
6.37 + * single choice of license, a recipient has the option to distribute
6.38 + * your version of this file under either the CDDL, the GPL Version 2 or
6.39 + * to extend the choice of license to its licensees as provided above.
6.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
6.41 + * Version 2 license, then the option applies only if the new code is
6.42 + * made subject to such option by the copyright holder.
6.43 + */
6.44 +
6.45 +
6.46 +package org.netbeans.modules.spring.lookup;
6.47 +
6.48 +import java.util.ArrayList;
6.49 +import java.util.Collection;
6.50 +import java.util.Collections;
6.51 +import java.util.LinkedHashMap;
6.52 +import java.util.LinkedHashSet;
6.53 +import java.util.List;
6.54 +import java.util.Map;
6.55 +import java.util.Map.Entry;
6.56 +import java.util.Set;
6.57 +import org.openide.util.Lookup;
6.58 +import org.openide.util.LookupListener;
6.59 +import org.springframework.beans.factory.BeanFactoryUtils;
6.60 +import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6.61 +import org.springframework.context.ApplicationContext;
6.62 +
6.63 +/**
6.64 + *
6.65 + * @author Andrei Badea
6.66 + */
6.67 +public class ApplicationContextBasedLookup extends Lookup {
6.68 +
6.69 + // TODO support other bean scopes (prototype, etc.)?
6.70 +
6.71 + private final ApplicationContext appContext;
6.72 +
6.73 + public ApplicationContextBasedLookup(ApplicationContext appContext) {
6.74 + this.appContext = appContext;
6.75 + }
6.76 +
6.77 + @Override
6.78 + public <T> T lookup(Class<T> clazz) {
6.79 + return lookupBean(clazz);
6.80 + }
6.81 +
6.82 + @Override
6.83 + public <T> Result<T> lookup(Template<T> template) {
6.84 + return new ResultImpl<T>(template);
6.85 + }
6.86 +
6.87 + private <T> T lookupBean(Class<T> type) {
6.88 + @SuppressWarnings("unchecked")
6.89 + Map<String, ? extends T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, type, false, true);
6.90 + for (T instance : beans.values()) {
6.91 + return instance;
6.92 + }
6.93 + return null;
6.94 + }
6.95 +
6.96 + private <T> T lookupBean(String id, Class<T> type) {
6.97 + try {
6.98 + @SuppressWarnings("unchecked")
6.99 + T bean = (T)appContext.getBean(id, type);
6.100 + return bean;
6.101 + } catch (NoSuchBeanDefinitionException e) {
6.102 + return null;
6.103 + }
6.104 + }
6.105 +
6.106 + private <T> Map<String, ? extends T> lookupBeans(Template<T> template) {
6.107 + String id = template.getId();
6.108 + Class<T> type = template.getType();
6.109 + T instance = template.getInstance();
6.110 + if (id != null) {
6.111 + T bean = lookupBean(id, type);
6.112 + if (instance != null && instance != bean) {
6.113 + bean = null;
6.114 + }
6.115 + return bean != null ? Collections.singletonMap(id, bean) : Collections.<String, T>emptyMap();
6.116 + } else {
6.117 + @SuppressWarnings("unchecked")
6.118 + Map<String, ? extends T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, type, false, true);
6.119 + if (instance != null) {
6.120 + Map<String, T> instanceMatch = new LinkedHashMap<String, T>();
6.121 + for (Entry<String, ? extends T> entry : beans.entrySet()) {
6.122 + if (entry.getValue() == instance) {
6.123 + instanceMatch.put(entry.getKey(), instance);
6.124 + }
6.125 + }
6.126 + beans = instanceMatch;
6.127 + }
6.128 + Map<String, T> computed = new LinkedHashMap<String, T>(beans);
6.129 + for (Entry<String, ? extends T> entry : beans.entrySet()) {
6.130 + for (String alias : appContext.getAliases(entry.getKey())) {
6.131 + computed.put(alias, entry.getValue());
6.132 + }
6.133 + }
6.134 + return computed;
6.135 + }
6.136 + }
6.137 +
6.138 + private final class ResultImpl<T> extends Result<T> {
6.139 +
6.140 + private final Template<T> template;
6.141 +
6.142 + // @GuardedBy("this")
6.143 + private Map<String, ? extends T> beans;
6.144 + // @GuardedBy("this")
6.145 + private List<Item<T>> items;
6.146 + // @GuardedBy("this")
6.147 + private Set<Class<? extends T>> classes;
6.148 + // @GuardedBy("this")
6.149 + private List<? extends T> instances;
6.150 +
6.151 + public ResultImpl(Template<T> template) {
6.152 + this.template = template;
6.153 + }
6.154 +
6.155 + @Override
6.156 + public synchronized Collection<? extends T> allInstances() {
6.157 + if (instances != null) {
6.158 + return instances;
6.159 + }
6.160 + List<T> newInstances = new ArrayList<T>();
6.161 + for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
6.162 + newInstances.add(entry.getValue());
6.163 + }
6.164 + instances = Collections.unmodifiableList(newInstances);
6.165 + return instances;
6.166 + }
6.167 +
6.168 + @Override
6.169 + public synchronized Set<Class<? extends T>> allClasses() {
6.170 + if (classes != null) {
6.171 + return classes;
6.172 + }
6.173 + Set<Class<? extends T>> newClasses = new LinkedHashSet<Class<? extends T>>();
6.174 + for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
6.175 + @SuppressWarnings("unchecked")
6.176 + Class<? extends T> clazz = (Class<? extends T>)entry.getValue().getClass();
6.177 + newClasses.add(clazz);
6.178 + }
6.179 + classes = Collections.<Class<? extends T>>unmodifiableSet(newClasses);
6.180 + return classes;
6.181 + }
6.182 +
6.183 + @Override
6.184 + public synchronized Collection<Item<T>> allItems() {
6.185 + if (items != null) {
6.186 + return items;
6.187 + }
6.188 + List<Item<T>> newItems = new ArrayList<Item<T>>();
6.189 + for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
6.190 + newItems.add(new ItemImpl<T>(entry.getKey(), entry.getValue()));
6.191 + }
6.192 + items = Collections.unmodifiableList(newItems);
6.193 + return newItems;
6.194 + }
6.195 +
6.196 + private synchronized Map<String, ? extends T> allBeans() {
6.197 + if (beans == null) {
6.198 + beans = lookupBeans(template);
6.199 + }
6.200 + return beans;
6.201 + }
6.202 +
6.203 + @Override
6.204 + public void addLookupListener(LookupListener l) {
6.205 + }
6.206 +
6.207 + @Override
6.208 + public void removeLookupListener(LookupListener l) {
6.209 + }
6.210 + }
6.211 +
6.212 + private static final class ItemImpl<T> extends Item<T> {
6.213 +
6.214 + private final T instance;
6.215 + private final String id;
6.216 +
6.217 + public ItemImpl(String id, T instance) {
6.218 + this.id = id != null ? id : "";
6.219 + assert instance != null;
6.220 + this.instance = instance;
6.221 + }
6.222 +
6.223 + @Override
6.224 + public T getInstance() {
6.225 + return instance;
6.226 + }
6.227 +
6.228 + @Override
6.229 + @SuppressWarnings("unchecked")
6.230 + public Class<? extends T> getType() {
6.231 + return (Class<? extends T>)instance.getClass();
6.232 + }
6.233 +
6.234 + @Override
6.235 + public String getId() {
6.236 + return id;
6.237 + }
6.238 +
6.239 + @Override
6.240 + public String getDisplayName() {
6.241 + return getId();
6.242 + }
6.243 + }
6.244 +}
6.245 \ No newline at end of file
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/spring.lookup/src/main/java/org/netbeans/modules/spring/lookup/LookupBasedApplicationContext.java Tue Apr 21 22:17:32 2009 +0200
7.3 @@ -0,0 +1,258 @@
7.4 +/*
7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7.6 + *
7.7 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
7.8 + *
7.9 + * The contents of this file are subject to the terms of either the GNU
7.10 + * General Public License Version 2 only ("GPL") or the Common
7.11 + * Development and Distribution License("CDDL") (collectively, the
7.12 + * "License"). You may not use this file except in compliance with the
7.13 + * License. You can obtain a copy of the License at
7.14 + * http://www.netbeans.org/cddl-gplv2.html
7.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
7.16 + * specific language governing permissions and limitations under the
7.17 + * License. When distributing the software, include this License Header
7.18 + * Notice in each file and include the License file at
7.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
7.20 + * particular file as subject to the "Classpath" exception as provided
7.21 + * by Sun in the GPL Version 2 section of the License file that
7.22 + * accompanied this code. If applicable, add the following below the
7.23 + * License Header, with the fields enclosed by brackets [] replaced by
7.24 + * your own identifying information:
7.25 + * "Portions Copyrighted [year] [name of copyright owner]"
7.26 + *
7.27 + * Contributor(s):
7.28 + *
7.29 + * The Original Software is NetBeans. The Initial Developer of the Original
7.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
7.31 + * Microsystems, Inc. All Rights Reserved.
7.32 + *
7.33 + * If you wish your version of this file to be governed by only the CDDL
7.34 + * or only the GPL Version 2, indicate your decision by adding
7.35 + * "[Contributor] elects to include this software in this distribution
7.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
7.37 + * single choice of license, a recipient has the option to distribute
7.38 + * your version of this file under either the CDDL, the GPL Version 2 or
7.39 + * to extend the choice of license to its licensees as provided above.
7.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
7.41 + * Version 2 license, then the option applies only if the new code is
7.42 + * made subject to such option by the copyright holder.
7.43 + */
7.44 +
7.45 +package org.netbeans.modules.spring.lookup;
7.46 +
7.47 +import java.io.IOException;
7.48 +import java.util.ArrayList;
7.49 +import java.util.HashMap;
7.50 +import java.util.List;
7.51 +import java.util.Locale;
7.52 +import java.util.Map;
7.53 +import org.openide.util.Lookup;
7.54 +import org.openide.util.Lookup.Item;
7.55 +import org.openide.util.Lookup.Template;
7.56 +import org.springframework.beans.BeansException;
7.57 +import org.springframework.beans.factory.BeanDefinitionStoreException;
7.58 +import org.springframework.beans.factory.BeanFactory;
7.59 +import org.springframework.beans.factory.NoSuchBeanDefinitionException;
7.60 +import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
7.61 +import org.springframework.context.ApplicationContext;
7.62 +import org.springframework.context.ApplicationEvent;
7.63 +import org.springframework.context.MessageSourceResolvable;
7.64 +import org.springframework.context.NoSuchMessageException;
7.65 +import org.springframework.core.io.DefaultResourceLoader;
7.66 +import org.springframework.core.io.Resource;
7.67 +import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
7.68 +import org.springframework.core.io.support.ResourcePatternResolver;
7.69 +
7.70 +/**
7.71 + *
7.72 + * @author Andrei Badea
7.73 + */
7.74 +@SuppressWarnings("unchecked")
7.75 +public class LookupBasedApplicationContext extends DefaultResourceLoader implements ApplicationContext {
7.76 +
7.77 + private final Lookup lookup;
7.78 + private final ResourcePatternResolver resourcePatternResolver;
7.79 + private final long startupDate = System.currentTimeMillis();
7.80 +
7.81 + public static ApplicationContext create(Lookup lookup) {
7.82 + ClassLoader loader = lookup.lookup(ClassLoader.class);
7.83 + if (loader == null) {
7.84 + throw new IllegalArgumentException();
7.85 + }
7.86 + return create(lookup, loader);
7.87 + }
7.88 +
7.89 + public static ApplicationContext create(Lookup lookup, ClassLoader loader) {
7.90 + return new LookupBasedApplicationContext(lookup, loader);
7.91 + }
7.92 +
7.93 + private LookupBasedApplicationContext(Lookup lookup, ClassLoader loader) {
7.94 + super(loader);
7.95 + this.lookup = lookup;
7.96 + this.resourcePatternResolver = new PathMatchingResourcePatternResolver(this);
7.97 + }
7.98 +
7.99 + // ListableBeanFactory methods.
7.100 +
7.101 + public String getId() {
7.102 + return "Id"; // XXX
7.103 + }
7.104 +
7.105 + public Object getBean(String name) throws BeansException {
7.106 + return getBean(name, (Class)null);
7.107 + }
7.108 +
7.109 + public Object getBean(String name, Class requiredType) throws BeansException {
7.110 + Item item = lookup.lookupItem(new Template(requiredType, name, null));
7.111 + return item != null ? item.getInstance() : null;
7.112 + }
7.113 +
7.114 + public Object getBean(String name, Object[] args) throws BeansException {
7.115 + Object instance = getBean(name);
7.116 + // Javadoc seems to imply that if arguments are given, a prototype bean
7.117 + // is expected.
7.118 + if (args.length == 0 && instance != null) {
7.119 + return instance;
7.120 + }
7.121 + if (instance != null) {
7.122 + throw new BeanDefinitionStoreException("No such bean " + name);
7.123 + }
7.124 + throw new NoSuchBeanDefinitionException(name);
7.125 + }
7.126 +
7.127 + public boolean containsBean(String name) {
7.128 + return getBean(name) != null;
7.129 + }
7.130 +
7.131 + public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
7.132 + if (containsBean(name)) {
7.133 + return true;
7.134 + }
7.135 + throw new NoSuchBeanDefinitionException(name);
7.136 + }
7.137 +
7.138 + public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
7.139 + if (containsBean(name)) {
7.140 + return false;
7.141 + }
7.142 + throw new NoSuchBeanDefinitionException(name);
7.143 + }
7.144 +
7.145 + public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
7.146 + Object bean = getBean(name);
7.147 + if (bean != null) {
7.148 + return targetType.isInstance(bean);
7.149 + }
7.150 + throw new NoSuchBeanDefinitionException(name);
7.151 + }
7.152 +
7.153 + public Class getType(String name) throws NoSuchBeanDefinitionException {
7.154 + Object bean = getBean(name);
7.155 + if (bean != null) {
7.156 + return bean.getClass();
7.157 + }
7.158 + throw new NoSuchBeanDefinitionException(name);
7.159 + }
7.160 +
7.161 + public String[] getAliases(String name) {
7.162 + return new String[0];
7.163 + }
7.164 +
7.165 + public boolean containsBeanDefinition(String beanName) {
7.166 + return containsBean(beanName);
7.167 + }
7.168 +
7.169 + public int getBeanDefinitionCount() {
7.170 + return lookup.lookupAll(Object.class).size();
7.171 + }
7.172 +
7.173 + public String[] getBeanDefinitionNames() {
7.174 + return getBeanNamesForType(Object.class);
7.175 + }
7.176 +
7.177 + public String[] getBeanNamesForType(Class type) {
7.178 + return getBeanNamesForType(type, false, false);
7.179 + }
7.180 +
7.181 + public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean allowEagerInit) {
7.182 + List<String> names = new ArrayList<String>();
7.183 + for (Item item : lookup.lookupResult((Class<?>)type).allItems()) {
7.184 + String name = item.getId();
7.185 + if (name != null) {
7.186 + names.add(name);
7.187 + }
7.188 + }
7.189 + return names.toArray(new String[names.size()]);
7.190 + }
7.191 +
7.192 + public Map getBeansOfType(Class type) throws BeansException {
7.193 + return getBeansOfType(type, false, false);
7.194 + }
7.195 +
7.196 + public Map getBeansOfType(Class type, boolean includePrototypes, boolean allowEagerInit) throws BeansException {
7.197 + Map map = new HashMap();
7.198 + for (Item item : lookup.lookupResult((Class<?>)type).allItems()) {
7.199 + String name = item.getId();
7.200 + if (name != null) {
7.201 + map.put(name, item.getInstance());
7.202 + }
7.203 + }
7.204 + return map;
7.205 + }
7.206 +
7.207 + // ApplicationContext methods.
7.208 +
7.209 + public ApplicationContext getParent() {
7.210 + return null;
7.211 + }
7.212 +
7.213 + public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
7.214 + throw new IllegalStateException("No autowire capable bean factory");
7.215 + }
7.216 +
7.217 + public String getDisplayName() {
7.218 + return null;
7.219 + }
7.220 +
7.221 + public long getStartupDate() {
7.222 + return startupDate;
7.223 + }
7.224 +
7.225 + // HierarchicalBeanFactory methods.
7.226 +
7.227 + public BeanFactory getParentBeanFactory() {
7.228 + return null;
7.229 + }
7.230 +
7.231 + public boolean containsLocalBean(String name) {
7.232 + return containsBean(name);
7.233 + }
7.234 +
7.235 + // MessageSource methods.
7.236 +
7.237 + public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
7.238 + throw new UnsupportedOperationException("Not supported yet.");
7.239 + }
7.240 +
7.241 + public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
7.242 + throw new UnsupportedOperationException("Not supported yet.");
7.243 + }
7.244 +
7.245 + public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
7.246 + throw new UnsupportedOperationException("Not supported yet.");
7.247 + }
7.248 +
7.249 + // ApplicationEventPublisher methods.
7.250 +
7.251 + public void publishEvent(ApplicationEvent event) {
7.252 + }
7.253 +
7.254 + // ResourcePatternResolver methods.
7.255 +
7.256 + public Resource[] getResources(String locationPattern) throws IOException {
7.257 + return resourcePatternResolver.getResources(locationPattern);
7.258 + }
7.259 +
7.260 + // ResourceLoader methods implemented in superclass.
7.261 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/spring.lookup/src/main/java/org/netbeans/modules/spring/lookup/resources/Bundle.properties Tue Apr 21 22:17:32 2009 +0200
8.3 @@ -0,0 +1,37 @@
8.4 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8.5 +#
8.6 +# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
8.7 +#
8.8 +# The contents of this file are subject to the terms of either the GNU
8.9 +# General Public License Version 2 only ("GPL") or the Common
8.10 +# Development and Distribution License("CDDL") (collectively, the
8.11 +# "License"). You may not use this file except in compliance with the
8.12 +# License. You can obtain a copy of the License at
8.13 +# http://www.netbeans.org/cddl-gplv2.html
8.14 +# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
8.15 +# specific language governing permissions and limitations under the
8.16 +# License. When distributing the software, include this License Header
8.17 +# Notice in each file and include the License file at
8.18 +# nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
8.19 +# particular file as subject to the "Classpath" exception as provided
8.20 +# by Sun in the GPL Version 2 section of the License file that
8.21 +# accompanied this code. If applicable, add the following below the
8.22 +# License Header, with the fields enclosed by brackets [] replaced by
8.23 +# your own identifying information:
8.24 +# "Portions Copyrighted [year] [name of copyright owner]"
8.25 +#
8.26 +# If you wish your version of this file to be governed by only the CDDL
8.27 +# or only the GPL Version 2, indicate your decision by adding
8.28 +# "[Contributor] elects to include this software in this distribution
8.29 +# under the [CDDL or GPL Version 2] license." If you do not indicate a
8.30 +# single choice of license, a recipient has the option to distribute
8.31 +# your version of this file under either the CDDL, the GPL Version 2 or
8.32 +# to extend the choice of license to its licensees as provided above.
8.33 +# However, if you add GPL Version 2 code and therefore, elected the GPL
8.34 +# Version 2 license, then the option applies only if the new code is
8.35 +# made subject to such option by the copyright holder.
8.36 +#
8.37 +# Contributor(s):
8.38 +#
8.39 +# Portions Copyrighted 2008 Sun Microsystems, Inc.
8.40 +OpenIDE-Module-Name=spring.lookup
9.1 --- a/spring.lookup/src/org/netbeans/api/spring/NbSpring.java Tue Apr 21 21:41:25 2009 +0200
9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
9.3 @@ -1,77 +0,0 @@
9.4 -/*
9.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9.6 - *
9.7 - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9.8 - *
9.9 - * The contents of this file are subject to the terms of either the GNU
9.10 - * General Public License Version 2 only ("GPL") or the Common
9.11 - * Development and Distribution License("CDDL") (collectively, the
9.12 - * "License"). You may not use this file except in compliance with the
9.13 - * License. You can obtain a copy of the License at
9.14 - * http://www.netbeans.org/cddl-gplv2.html
9.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
9.16 - * specific language governing permissions and limitations under the
9.17 - * License. When distributing the software, include this License Header
9.18 - * Notice in each file and include the License file at
9.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
9.20 - * particular file as subject to the "Classpath" exception as provided
9.21 - * by Sun in the GPL Version 2 section of the License file that
9.22 - * accompanied this code. If applicable, add the following below the
9.23 - * License Header, with the fields enclosed by brackets [] replaced by
9.24 - * your own identifying information:
9.25 - * "Portions Copyrighted [year] [name of copyright owner]"
9.26 - *
9.27 - * If you wish your version of this file to be governed by only the CDDL
9.28 - * or only the GPL Version 2, indicate your decision by adding
9.29 - * "[Contributor] elects to include this software in this distribution
9.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
9.31 - * single choice of license, a recipient has the option to distribute
9.32 - * your version of this file under either the CDDL, the GPL Version 2 or
9.33 - * to extend the choice of license to its licensees as provided above.
9.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
9.35 - * Version 2 license, then the option applies only if the new code is
9.36 - * made subject to such option by the copyright holder.
9.37 - *
9.38 - * Contributor(s):
9.39 - *
9.40 - * Portions Copyrighted 2008 Sun Microsystems, Inc.
9.41 - */
9.42 -
9.43 -package org.netbeans.api.spring;
9.44 -
9.45 -import org.netbeans.modules.spring.lookup.ApplicationContextBasedLookup;
9.46 -import org.netbeans.modules.spring.lookup.LookupBasedApplicationContext;
9.47 -import org.openide.util.Lookup;
9.48 -import org.springframework.context.ApplicationContext;
9.49 -
9.50 -/** Factory methods to manipulate and bridge NetBeans Lookup and SpringFramework's
9.51 - * ApplicationContext.
9.52 - * @author Jaroslav Tulach, Andrei Badea
9.53 - * @since 1.1
9.54 - */
9.55 -public class NbSpring {
9.56 - private NbSpring() {
9.57 - }
9.58 -
9.59 - /** Converts an application context to lookup.
9.60 - *
9.61 - * @param context application context
9.62 - * @return lookup that bridges to the context
9.63 - */
9.64 - public static Lookup create(ApplicationContext context) {
9.65 - return new ApplicationContextBasedLookup(context);
9.66 - }
9.67 -
9.68 - /** From lookup to application context, if necesary.
9.69 - *
9.70 - * @param context application context
9.71 - * @return lookup that bridges to the context
9.72 - */
9.73 - public static ApplicationContext create(Lookup lookup) {
9.74 - ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
9.75 - if (l == null) {
9.76 - l = NbSpring.class.getClassLoader();
9.77 - }
9.78 - return LookupBasedApplicationContext.create(lookup, l);
9.79 - }
9.80 -}
10.1 --- a/spring.lookup/src/org/netbeans/modules/spring/lookup/ApplicationContextBasedLookup.java Tue Apr 21 21:41:25 2009 +0200
10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
10.3 @@ -1,241 +0,0 @@
10.4 -/*
10.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
10.6 - *
10.7 - * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
10.8 - *
10.9 - * The contents of this file are subject to the terms of either the GNU
10.10 - * General Public License Version 2 only ("GPL") or the Common
10.11 - * Development and Distribution License("CDDL") (collectively, the
10.12 - * "License"). You may not use this file except in compliance with the
10.13 - * License. You can obtain a copy of the License at
10.14 - * http://www.netbeans.org/cddl-gplv2.html
10.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
10.16 - * specific language governing permissions and limitations under the
10.17 - * License. When distributing the software, include this License Header
10.18 - * Notice in each file and include the License file at
10.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
10.20 - * particular file as subject to the "Classpath" exception as provided
10.21 - * by Sun in the GPL Version 2 section of the License file that
10.22 - * accompanied this code. If applicable, add the following below the
10.23 - * License Header, with the fields enclosed by brackets [] replaced by
10.24 - * your own identifying information:
10.25 - * "Portions Copyrighted [year] [name of copyright owner]"
10.26 - *
10.27 - * Contributor(s):
10.28 - *
10.29 - * The Original Software is NetBeans. The Initial Developer of the Original
10.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
10.31 - * Microsystems, Inc. All Rights Reserved.
10.32 - *
10.33 - * If you wish your version of this file to be governed by only the CDDL
10.34 - * or only the GPL Version 2, indicate your decision by adding
10.35 - * "[Contributor] elects to include this software in this distribution
10.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
10.37 - * single choice of license, a recipient has the option to distribute
10.38 - * your version of this file under either the CDDL, the GPL Version 2 or
10.39 - * to extend the choice of license to its licensees as provided above.
10.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
10.41 - * Version 2 license, then the option applies only if the new code is
10.42 - * made subject to such option by the copyright holder.
10.43 - */
10.44 -
10.45 -
10.46 -package org.netbeans.modules.spring.lookup;
10.47 -
10.48 -import java.util.ArrayList;
10.49 -import java.util.Collection;
10.50 -import java.util.Collections;
10.51 -import java.util.LinkedHashMap;
10.52 -import java.util.LinkedHashSet;
10.53 -import java.util.List;
10.54 -import java.util.Map;
10.55 -import java.util.Map.Entry;
10.56 -import java.util.Set;
10.57 -import org.openide.util.Lookup;
10.58 -import org.openide.util.LookupListener;
10.59 -import org.springframework.beans.factory.BeanFactoryUtils;
10.60 -import org.springframework.beans.factory.NoSuchBeanDefinitionException;
10.61 -import org.springframework.context.ApplicationContext;
10.62 -
10.63 -/**
10.64 - *
10.65 - * @author Andrei Badea
10.66 - */
10.67 -public class ApplicationContextBasedLookup extends Lookup {
10.68 -
10.69 - // TODO support other bean scopes (prototype, etc.)?
10.70 -
10.71 - private final ApplicationContext appContext;
10.72 -
10.73 - public ApplicationContextBasedLookup(ApplicationContext appContext) {
10.74 - this.appContext = appContext;
10.75 - }
10.76 -
10.77 - @Override
10.78 - public <T> T lookup(Class<T> clazz) {
10.79 - return lookupBean(clazz);
10.80 - }
10.81 -
10.82 - @Override
10.83 - public <T> Result<T> lookup(Template<T> template) {
10.84 - return new ResultImpl<T>(template);
10.85 - }
10.86 -
10.87 - private <T> T lookupBean(Class<T> type) {
10.88 - @SuppressWarnings("unchecked")
10.89 - Map<String, ? extends T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, type, false, true);
10.90 - for (T instance : beans.values()) {
10.91 - return instance;
10.92 - }
10.93 - return null;
10.94 - }
10.95 -
10.96 - private <T> T lookupBean(String id, Class<T> type) {
10.97 - try {
10.98 - @SuppressWarnings("unchecked")
10.99 - T bean = (T)appContext.getBean(id, type);
10.100 - return bean;
10.101 - } catch (NoSuchBeanDefinitionException e) {
10.102 - return null;
10.103 - }
10.104 - }
10.105 -
10.106 - private <T> Map<String, ? extends T> lookupBeans(Template<T> template) {
10.107 - String id = template.getId();
10.108 - Class<T> type = template.getType();
10.109 - T instance = template.getInstance();
10.110 - if (id != null) {
10.111 - T bean = lookupBean(id, type);
10.112 - if (instance != null && instance != bean) {
10.113 - bean = null;
10.114 - }
10.115 - return bean != null ? Collections.singletonMap(id, bean) : Collections.<String, T>emptyMap();
10.116 - } else {
10.117 - @SuppressWarnings("unchecked")
10.118 - Map<String, ? extends T> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, type, false, true);
10.119 - if (instance != null) {
10.120 - Map<String, T> instanceMatch = new LinkedHashMap<String, T>();
10.121 - for (Entry<String, ? extends T> entry : beans.entrySet()) {
10.122 - if (entry.getValue() == instance) {
10.123 - instanceMatch.put(entry.getKey(), instance);
10.124 - }
10.125 - }
10.126 - beans = instanceMatch;
10.127 - }
10.128 - Map<String, T> computed = new LinkedHashMap<String, T>(beans);
10.129 - for (Entry<String, ? extends T> entry : beans.entrySet()) {
10.130 - for (String alias : appContext.getAliases(entry.getKey())) {
10.131 - computed.put(alias, entry.getValue());
10.132 - }
10.133 - }
10.134 - return computed;
10.135 - }
10.136 - }
10.137 -
10.138 - private final class ResultImpl<T> extends Result<T> {
10.139 -
10.140 - private final Template<T> template;
10.141 -
10.142 - // @GuardedBy("this")
10.143 - private Map<String, ? extends T> beans;
10.144 - // @GuardedBy("this")
10.145 - private List<Item<T>> items;
10.146 - // @GuardedBy("this")
10.147 - private Set<Class<? extends T>> classes;
10.148 - // @GuardedBy("this")
10.149 - private List<? extends T> instances;
10.150 -
10.151 - public ResultImpl(Template<T> template) {
10.152 - this.template = template;
10.153 - }
10.154 -
10.155 - @Override
10.156 - public synchronized Collection<? extends T> allInstances() {
10.157 - if (instances != null) {
10.158 - return instances;
10.159 - }
10.160 - List<T> newInstances = new ArrayList<T>();
10.161 - for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
10.162 - newInstances.add(entry.getValue());
10.163 - }
10.164 - instances = Collections.unmodifiableList(newInstances);
10.165 - return instances;
10.166 - }
10.167 -
10.168 - @Override
10.169 - public synchronized Set<Class<? extends T>> allClasses() {
10.170 - if (classes != null) {
10.171 - return classes;
10.172 - }
10.173 - Set<Class<? extends T>> newClasses = new LinkedHashSet<Class<? extends T>>();
10.174 - for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
10.175 - @SuppressWarnings("unchecked")
10.176 - Class<? extends T> clazz = (Class<? extends T>)entry.getValue().getClass();
10.177 - newClasses.add(clazz);
10.178 - }
10.179 - classes = Collections.<Class<? extends T>>unmodifiableSet(newClasses);
10.180 - return classes;
10.181 - }
10.182 -
10.183 - @Override
10.184 - public synchronized Collection<Item<T>> allItems() {
10.185 - if (items != null) {
10.186 - return items;
10.187 - }
10.188 - List<Item<T>> newItems = new ArrayList<Item<T>>();
10.189 - for (Entry<String, ? extends T> entry : allBeans().entrySet()) {
10.190 - newItems.add(new ItemImpl<T>(entry.getKey(), entry.getValue()));
10.191 - }
10.192 - items = Collections.unmodifiableList(newItems);
10.193 - return newItems;
10.194 - }
10.195 -
10.196 - private synchronized Map<String, ? extends T> allBeans() {
10.197 - if (beans == null) {
10.198 - beans = lookupBeans(template);
10.199 - }
10.200 - return beans;
10.201 - }
10.202 -
10.203 - @Override
10.204 - public void addLookupListener(LookupListener l) {
10.205 - }
10.206 -
10.207 - @Override
10.208 - public void removeLookupListener(LookupListener l) {
10.209 - }
10.210 - }
10.211 -
10.212 - private static final class ItemImpl<T> extends Item<T> {
10.213 -
10.214 - private final T instance;
10.215 - private final String id;
10.216 -
10.217 - public ItemImpl(String id, T instance) {
10.218 - this.id = id != null ? id : "";
10.219 - assert instance != null;
10.220 - this.instance = instance;
10.221 - }
10.222 -
10.223 - @Override
10.224 - public T getInstance() {
10.225 - return instance;
10.226 - }
10.227 -
10.228 - @Override
10.229 - @SuppressWarnings("unchecked")
10.230 - public Class<? extends T> getType() {
10.231 - return (Class<? extends T>)instance.getClass();
10.232 - }
10.233 -
10.234 - @Override
10.235 - public String getId() {
10.236 - return id;
10.237 - }
10.238 -
10.239 - @Override
10.240 - public String getDisplayName() {
10.241 - return getId();
10.242 - }
10.243 - }
10.244 -}
10.245 \ No newline at end of file
11.1 --- a/spring.lookup/src/org/netbeans/modules/spring/lookup/LookupBasedApplicationContext.java Tue Apr 21 21:41:25 2009 +0200
11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
11.3 @@ -1,254 +0,0 @@
11.4 -/*
11.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
11.6 - *
11.7 - * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
11.8 - *
11.9 - * The contents of this file are subject to the terms of either the GNU
11.10 - * General Public License Version 2 only ("GPL") or the Common
11.11 - * Development and Distribution License("CDDL") (collectively, the
11.12 - * "License"). You may not use this file except in compliance with the
11.13 - * License. You can obtain a copy of the License at
11.14 - * http://www.netbeans.org/cddl-gplv2.html
11.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11.16 - * specific language governing permissions and limitations under the
11.17 - * License. When distributing the software, include this License Header
11.18 - * Notice in each file and include the License file at
11.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
11.20 - * particular file as subject to the "Classpath" exception as provided
11.21 - * by Sun in the GPL Version 2 section of the License file that
11.22 - * accompanied this code. If applicable, add the following below the
11.23 - * License Header, with the fields enclosed by brackets [] replaced by
11.24 - * your own identifying information:
11.25 - * "Portions Copyrighted [year] [name of copyright owner]"
11.26 - *
11.27 - * Contributor(s):
11.28 - *
11.29 - * The Original Software is NetBeans. The Initial Developer of the Original
11.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
11.31 - * Microsystems, Inc. All Rights Reserved.
11.32 - *
11.33 - * If you wish your version of this file to be governed by only the CDDL
11.34 - * or only the GPL Version 2, indicate your decision by adding
11.35 - * "[Contributor] elects to include this software in this distribution
11.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
11.37 - * single choice of license, a recipient has the option to distribute
11.38 - * your version of this file under either the CDDL, the GPL Version 2 or
11.39 - * to extend the choice of license to its licensees as provided above.
11.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
11.41 - * Version 2 license, then the option applies only if the new code is
11.42 - * made subject to such option by the copyright holder.
11.43 - */
11.44 -
11.45 -package org.netbeans.modules.spring.lookup;
11.46 -
11.47 -import java.io.IOException;
11.48 -import java.util.ArrayList;
11.49 -import java.util.HashMap;
11.50 -import java.util.List;
11.51 -import java.util.Locale;
11.52 -import java.util.Map;
11.53 -import org.openide.util.Lookup;
11.54 -import org.openide.util.Lookup.Item;
11.55 -import org.openide.util.Lookup.Template;
11.56 -import org.springframework.beans.BeansException;
11.57 -import org.springframework.beans.factory.BeanDefinitionStoreException;
11.58 -import org.springframework.beans.factory.BeanFactory;
11.59 -import org.springframework.beans.factory.NoSuchBeanDefinitionException;
11.60 -import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
11.61 -import org.springframework.context.ApplicationContext;
11.62 -import org.springframework.context.ApplicationEvent;
11.63 -import org.springframework.context.MessageSourceResolvable;
11.64 -import org.springframework.context.NoSuchMessageException;
11.65 -import org.springframework.core.io.DefaultResourceLoader;
11.66 -import org.springframework.core.io.Resource;
11.67 -import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
11.68 -import org.springframework.core.io.support.ResourcePatternResolver;
11.69 -
11.70 -/**
11.71 - *
11.72 - * @author Andrei Badea
11.73 - */
11.74 -@SuppressWarnings("unchecked")
11.75 -public class LookupBasedApplicationContext extends DefaultResourceLoader implements ApplicationContext {
11.76 -
11.77 - private final Lookup lookup;
11.78 - private final ResourcePatternResolver resourcePatternResolver;
11.79 - private final long startupDate = System.currentTimeMillis();
11.80 -
11.81 - public static ApplicationContext create(Lookup lookup) {
11.82 - ClassLoader loader = lookup.lookup(ClassLoader.class);
11.83 - if (loader == null) {
11.84 - throw new IllegalArgumentException();
11.85 - }
11.86 - return create(lookup, loader);
11.87 - }
11.88 -
11.89 - public static ApplicationContext create(Lookup lookup, ClassLoader loader) {
11.90 - return new LookupBasedApplicationContext(lookup, loader);
11.91 - }
11.92 -
11.93 - private LookupBasedApplicationContext(Lookup lookup, ClassLoader loader) {
11.94 - super(loader);
11.95 - this.lookup = lookup;
11.96 - this.resourcePatternResolver = new PathMatchingResourcePatternResolver(this);
11.97 - }
11.98 -
11.99 - // ListableBeanFactory methods.
11.100 -
11.101 - public Object getBean(String name) throws BeansException {
11.102 - return getBean(name, (Class)null);
11.103 - }
11.104 -
11.105 - public Object getBean(String name, Class requiredType) throws BeansException {
11.106 - Item item = lookup.lookupItem(new Template(requiredType, name, null));
11.107 - return item != null ? item.getInstance() : null;
11.108 - }
11.109 -
11.110 - public Object getBean(String name, Object[] args) throws BeansException {
11.111 - Object instance = getBean(name);
11.112 - // Javadoc seems to imply that if arguments are given, a prototype bean
11.113 - // is expected.
11.114 - if (args.length == 0 && instance != null) {
11.115 - return instance;
11.116 - }
11.117 - if (instance != null) {
11.118 - throw new BeanDefinitionStoreException("No such bean " + name);
11.119 - }
11.120 - throw new NoSuchBeanDefinitionException(name);
11.121 - }
11.122 -
11.123 - public boolean containsBean(String name) {
11.124 - return getBean(name) != null;
11.125 - }
11.126 -
11.127 - public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
11.128 - if (containsBean(name)) {
11.129 - return true;
11.130 - }
11.131 - throw new NoSuchBeanDefinitionException(name);
11.132 - }
11.133 -
11.134 - public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
11.135 - if (containsBean(name)) {
11.136 - return false;
11.137 - }
11.138 - throw new NoSuchBeanDefinitionException(name);
11.139 - }
11.140 -
11.141 - public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
11.142 - Object bean = getBean(name);
11.143 - if (bean != null) {
11.144 - return targetType.isInstance(bean);
11.145 - }
11.146 - throw new NoSuchBeanDefinitionException(name);
11.147 - }
11.148 -
11.149 - public Class getType(String name) throws NoSuchBeanDefinitionException {
11.150 - Object bean = getBean(name);
11.151 - if (bean != null) {
11.152 - return bean.getClass();
11.153 - }
11.154 - throw new NoSuchBeanDefinitionException(name);
11.155 - }
11.156 -
11.157 - public String[] getAliases(String name) {
11.158 - return new String[0];
11.159 - }
11.160 -
11.161 - public boolean containsBeanDefinition(String beanName) {
11.162 - return containsBean(beanName);
11.163 - }
11.164 -
11.165 - public int getBeanDefinitionCount() {
11.166 - return lookup.lookupAll(Object.class).size();
11.167 - }
11.168 -
11.169 - public String[] getBeanDefinitionNames() {
11.170 - return getBeanNamesForType(Object.class);
11.171 - }
11.172 -
11.173 - public String[] getBeanNamesForType(Class type) {
11.174 - return getBeanNamesForType(type, false, false);
11.175 - }
11.176 -
11.177 - public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean allowEagerInit) {
11.178 - List<String> names = new ArrayList<String>();
11.179 - for (Item item : lookup.lookupResult((Class<?>)type).allItems()) {
11.180 - String name = item.getId();
11.181 - if (name != null) {
11.182 - names.add(name);
11.183 - }
11.184 - }
11.185 - return names.toArray(new String[names.size()]);
11.186 - }
11.187 -
11.188 - public Map getBeansOfType(Class type) throws BeansException {
11.189 - return getBeansOfType(type, false, false);
11.190 - }
11.191 -
11.192 - public Map getBeansOfType(Class type, boolean includePrototypes, boolean allowEagerInit) throws BeansException {
11.193 - Map map = new HashMap();
11.194 - for (Item item : lookup.lookupResult((Class<?>)type).allItems()) {
11.195 - String name = item.getId();
11.196 - if (name != null) {
11.197 - map.put(name, item.getInstance());
11.198 - }
11.199 - }
11.200 - return map;
11.201 - }
11.202 -
11.203 - // ApplicationContext methods.
11.204 -
11.205 - public ApplicationContext getParent() {
11.206 - return null;
11.207 - }
11.208 -
11.209 - public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
11.210 - throw new IllegalStateException("No autowire capable bean factory");
11.211 - }
11.212 -
11.213 - public String getDisplayName() {
11.214 - return null;
11.215 - }
11.216 -
11.217 - public long getStartupDate() {
11.218 - return startupDate;
11.219 - }
11.220 -
11.221 - // HierarchicalBeanFactory methods.
11.222 -
11.223 - public BeanFactory getParentBeanFactory() {
11.224 - return null;
11.225 - }
11.226 -
11.227 - public boolean containsLocalBean(String name) {
11.228 - return containsBean(name);
11.229 - }
11.230 -
11.231 - // MessageSource methods.
11.232 -
11.233 - public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
11.234 - throw new UnsupportedOperationException("Not supported yet.");
11.235 - }
11.236 -
11.237 - public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException {
11.238 - throw new UnsupportedOperationException("Not supported yet.");
11.239 - }
11.240 -
11.241 - public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
11.242 - throw new UnsupportedOperationException("Not supported yet.");
11.243 - }
11.244 -
11.245 - // ApplicationEventPublisher methods.
11.246 -
11.247 - public void publishEvent(ApplicationEvent event) {
11.248 - }
11.249 -
11.250 - // ResourcePatternResolver methods.
11.251 -
11.252 - public Resource[] getResources(String locationPattern) throws IOException {
11.253 - return resourcePatternResolver.getResources(locationPattern);
11.254 - }
11.255 -
11.256 - // ResourceLoader methods implemented in superclass.
11.257 -}
12.1 --- a/spring.lookup/src/org/netbeans/modules/spring/lookup/resources/Bundle.properties Tue Apr 21 21:41:25 2009 +0200
12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
12.3 @@ -1,37 +0,0 @@
12.4 -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
12.5 -#
12.6 -# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
12.7 -#
12.8 -# The contents of this file are subject to the terms of either the GNU
12.9 -# General Public License Version 2 only ("GPL") or the Common
12.10 -# Development and Distribution License("CDDL") (collectively, the
12.11 -# "License"). You may not use this file except in compliance with the
12.12 -# License. You can obtain a copy of the License at
12.13 -# http://www.netbeans.org/cddl-gplv2.html
12.14 -# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
12.15 -# specific language governing permissions and limitations under the
12.16 -# License. When distributing the software, include this License Header
12.17 -# Notice in each file and include the License file at
12.18 -# nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
12.19 -# particular file as subject to the "Classpath" exception as provided
12.20 -# by Sun in the GPL Version 2 section of the License file that
12.21 -# accompanied this code. If applicable, add the following below the
12.22 -# License Header, with the fields enclosed by brackets [] replaced by
12.23 -# your own identifying information:
12.24 -# "Portions Copyrighted [year] [name of copyright owner]"
12.25 -#
12.26 -# If you wish your version of this file to be governed by only the CDDL
12.27 -# or only the GPL Version 2, indicate your decision by adding
12.28 -# "[Contributor] elects to include this software in this distribution
12.29 -# under the [CDDL or GPL Version 2] license." If you do not indicate a
12.30 -# single choice of license, a recipient has the option to distribute
12.31 -# your version of this file under either the CDDL, the GPL Version 2 or
12.32 -# to extend the choice of license to its licensees as provided above.
12.33 -# However, if you add GPL Version 2 code and therefore, elected the GPL
12.34 -# Version 2 license, then the option applies only if the new code is
12.35 -# made subject to such option by the copyright holder.
12.36 -#
12.37 -# Contributor(s):
12.38 -#
12.39 -# Portions Copyrighted 2008 Sun Microsystems, Inc.
12.40 -OpenIDE-Module-Name=spring.lookup
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/spring.lookup/src/test/java/org/netbeans/modules/spring/lookup/ApplicationContextBasedLookupTest.java Tue Apr 21 22:17:32 2009 +0200
13.3 @@ -0,0 +1,155 @@
13.4 +/*
13.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
13.6 + *
13.7 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
13.8 + *
13.9 + * The contents of this file are subject to the terms of either the GNU
13.10 + * General Public License Version 2 only ("GPL") or the Common
13.11 + * Development and Distribution License("CDDL") (collectively, the
13.12 + * "License"). You may not use this file except in compliance with the
13.13 + * License. You can obtain a copy of the License at
13.14 + * http://www.netbeans.org/cddl-gplv2.html
13.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13.16 + * specific language governing permissions and limitations under the
13.17 + * License. When distributing the software, include this License Header
13.18 + * Notice in each file and include the License file at
13.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
13.20 + * particular file as subject to the "Classpath" exception as provided
13.21 + * by Sun in the GPL Version 2 section of the License file that
13.22 + * accompanied this code. If applicable, add the following below the
13.23 + * License Header, with the fields enclosed by brackets [] replaced by
13.24 + * your own identifying information:
13.25 + * "Portions Copyrighted [year] [name of copyright owner]"
13.26 + *
13.27 + * Contributor(s):
13.28 + *
13.29 + * The Original Software is NetBeans. The Initial Developer of the Original
13.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
13.31 + * Microsystems, Inc. All Rights Reserved.
13.32 + *
13.33 + * If you wish your version of this file to be governed by only the CDDL
13.34 + * or only the GPL Version 2, indicate your decision by adding
13.35 + * "[Contributor] elects to include this software in this distribution
13.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
13.37 + * single choice of license, a recipient has the option to distribute
13.38 + * your version of this file under either the CDDL, the GPL Version 2 or
13.39 + * to extend the choice of license to its licensees as provided above.
13.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
13.41 + * Version 2 license, then the option applies only if the new code is
13.42 + * made subject to such option by the copyright holder.
13.43 + */
13.44 +
13.45 +package org.netbeans.modules.spring.lookup;
13.46 +
13.47 +import java.util.ArrayList;
13.48 +import java.util.HashMap;
13.49 +import java.util.HashSet;
13.50 +import java.util.Iterator;
13.51 +import java.util.LinkedList;
13.52 +import java.util.List;
13.53 +import java.util.Map;
13.54 +import java.util.Set;
13.55 +import junit.framework.TestCase;
13.56 +import org.openide.util.Lookup;
13.57 +import org.openide.util.Lookup.Item;
13.58 +import org.openide.util.Lookup.Result;
13.59 +import org.openide.util.Lookup.Template;
13.60 +import org.springframework.context.ApplicationContext;
13.61 +
13.62 +/**
13.63 + *
13.64 + * @author Andrei Badea
13.65 + */
13.66 +public class ApplicationContextBasedLookupTest extends TestCase {
13.67 +
13.68 + public ApplicationContextBasedLookupTest(String testName) {
13.69 + super(testName);
13.70 + }
13.71 +
13.72 + public void testLookupSingle() throws Exception {
13.73 + ApplicationContext ctx = TestUtils.createAppContext(
13.74 + "<bean id='foo' class='java.util.ArrayList'/>" +
13.75 + "<bean id='baz' class='java.util.HashMap'/>");
13.76 + ArrayList foo = (ArrayList)ctx.getBean("foo");
13.77 + HashMap baz = (HashMap)ctx.getBean("baz");
13.78 + Lookup lookup = new ApplicationContextBasedLookup(ctx);
13.79 +
13.80 + assertSame(foo, lookup.lookup(List.class));
13.81 + assertSame(foo, lookup.lookup(ArrayList.class));
13.82 + assertSame(baz, lookup.lookup(Map.class));
13.83 + // Order should be preserved.
13.84 + assertSame(foo, lookup.lookup(Object.class));
13.85 + }
13.86 +
13.87 + public void testItem() throws Exception {
13.88 + ApplicationContext ctx = TestUtils.createAppContext(
13.89 + "<bean id='foo' class='java.util.ArrayList'/>");
13.90 + ArrayList foo = (ArrayList)ctx.getBean("foo");
13.91 + Lookup lookup = new ApplicationContextBasedLookup(ctx);
13.92 +
13.93 + Item item = lookup.lookupItem(new Template<List>(List.class, null, null));
13.94 + assertEquals("foo", item.getId());
13.95 + assertEquals("foo", item.getDisplayName());
13.96 + assertSame(ArrayList.class, item.getType());
13.97 + assertSame(foo, item.getInstance());
13.98 + }
13.99 +
13.100 + public void testLookupResult() throws Exception {
13.101 + ApplicationContext ctx = TestUtils.createAppContext(
13.102 + "<bean id='foo' class='java.util.ArrayList'/>" +
13.103 + "<bean id='bar' class='java.util.LinkedList'/>" +
13.104 + "<bean id='baz' class='java.util.HashMap'/>");
13.105 + ArrayList foo = (ArrayList)ctx.getBean("foo");
13.106 + LinkedList bar = (LinkedList)ctx.getBean("bar");
13.107 + HashMap baz = (HashMap)ctx.getBean("baz");
13.108 + Lookup lookup = new ApplicationContextBasedLookup(ctx);
13.109 +
13.110 + assertSame(foo, lookup.lookupItem(new Template<List>(List.class, null, foo)).getInstance());
13.111 + assertSame(bar, lookup.lookupItem(new Template<List>(List.class, "bar", bar)).getInstance());
13.112 +
13.113 + assertNull(lookup.lookupItem(new Template<List>(List.class, "none", null)));
13.114 + assertNull(lookup.lookupItem(new Template<List>(List.class, "foo", bar)));
13.115 +
13.116 + Result<List> listResult = lookup.lookupResult(List.class);
13.117 + Iterator<Class<? extends List>> listClassIterator = listResult.allClasses().iterator();
13.118 + assertSame(ArrayList.class, listClassIterator.next());
13.119 + assertSame(LinkedList.class, listClassIterator.next());
13.120 + assertFalse(listClassIterator.hasNext());
13.121 + Iterator<? extends List> listIterator = listResult.allInstances().iterator();
13.122 + assertSame(foo, listIterator.next());
13.123 + assertSame(bar, listIterator.next());
13.124 + assertFalse(listIterator.hasNext());
13.125 + }
13.126 +
13.127 + public void testAliases() throws Exception {
13.128 + ApplicationContext ctx = TestUtils.createAppContext(
13.129 + "<alias name='foo' alias='bar'/>" +
13.130 + "<bean id='foo' class='java.util.ArrayList'/>" +
13.131 + "<bean id='baz' class='java.util.HashMap'/>");
13.132 + ArrayList foo = (ArrayList)ctx.getBean("foo");
13.133 + Lookup lookup = new ApplicationContextBasedLookup(ctx);
13.134 +
13.135 + assertSame(foo, lookup.lookupItem(new Template<List>(List.class, "bar", null)).getInstance());
13.136 +
13.137 + Result<List> result = lookup.lookup(new Template<List>(List.class, null, foo));
13.138 + Iterator<? extends Item<List>> iterator = result.allItems().iterator();
13.139 + Set<String> ids = new HashSet<String>();
13.140 + Item item = iterator.next();
13.141 + assertSame(foo, item.getInstance());
13.142 + ids.add(item.getId());
13.143 + item = iterator.next();
13.144 + assertSame(foo, item.getInstance());
13.145 + ids.add(item.getId());
13.146 + assertEquals(2, ids.size());
13.147 + assertTrue(ids.contains("foo"));
13.148 + assertTrue(ids.contains("bar"));
13.149 + }
13.150 +
13.151 + public void testPrototypesIgnored() throws Exception {
13.152 + ApplicationContext ctx = TestUtils.createAppContext(
13.153 + "<bean id='foo' class='java.util.ArrayList' scope='prototype'/>");
13.154 + Lookup lookup = new ApplicationContextBasedLookup(ctx);
13.155 +
13.156 + assertNull(lookup.lookup(List.class));
13.157 + }
13.158 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/spring.lookup/src/test/java/org/netbeans/modules/spring/lookup/LookupBasedApplicationContextTest.java Tue Apr 21 22:17:32 2009 +0200
14.3 @@ -0,0 +1,184 @@
14.4 +/*
14.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
14.6 + *
14.7 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
14.8 + *
14.9 + * The contents of this file are subject to the terms of either the GNU
14.10 + * General Public License Version 2 only ("GPL") or the Common
14.11 + * Development and Distribution License("CDDL") (collectively, the
14.12 + * "License"). You may not use this file except in compliance with the
14.13 + * License. You can obtain a copy of the License at
14.14 + * http://www.netbeans.org/cddl-gplv2.html
14.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
14.16 + * specific language governing permissions and limitations under the
14.17 + * License. When distributing the software, include this License Header
14.18 + * Notice in each file and include the License file at
14.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
14.20 + * particular file as subject to the "Classpath" exception as provided
14.21 + * by Sun in the GPL Version 2 section of the License file that
14.22 + * accompanied this code. If applicable, add the following below the
14.23 + * License Header, with the fields enclosed by brackets [] replaced by
14.24 + * your own identifying information:
14.25 + * "Portions Copyrighted [year] [name of copyright owner]"
14.26 + *
14.27 + * Contributor(s):
14.28 + *
14.29 + * The Original Software is NetBeans. The Initial Developer of the Original
14.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
14.31 + * Microsystems, Inc. All Rights Reserved.
14.32 + *
14.33 + * If you wish your version of this file to be governed by only the CDDL
14.34 + * or only the GPL Version 2, indicate your decision by adding
14.35 + * "[Contributor] elects to include this software in this distribution
14.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
14.37 + * single choice of license, a recipient has the option to distribute
14.38 + * your version of this file under either the CDDL, the GPL Version 2 or
14.39 + * to extend the choice of license to its licensees as provided above.
14.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
14.41 + * Version 2 license, then the option applies only if the new code is
14.42 + * made subject to such option by the copyright holder.
14.43 + */
14.44 +
14.45 +package org.netbeans.modules.spring.lookup;
14.46 +
14.47 +import java.util.ArrayList;
14.48 +import java.util.HashMap;
14.49 +import java.util.LinkedList;
14.50 +import java.util.List;
14.51 +import java.util.Map;
14.52 +import junit.framework.TestCase;
14.53 +import org.openide.util.Lookup;
14.54 +import org.openide.util.lookup.AbstractLookup;
14.55 +import org.openide.util.lookup.AbstractLookup.Content;
14.56 +import org.openide.util.lookup.AbstractLookup.Pair;
14.57 +import org.springframework.beans.factory.BeanDefinitionStoreException;
14.58 +import org.springframework.beans.factory.NoSuchBeanDefinitionException;
14.59 +import org.springframework.context.ApplicationContext;
14.60 +
14.61 +/**
14.62 + *
14.63 + * @author Andrei Badea
14.64 + */
14.65 +public class LookupBasedApplicationContextTest extends TestCase {
14.66 +
14.67 + // TODO test others ApplicationContext methods.
14.68 +
14.69 + public LookupBasedApplicationContextTest(String testName) {
14.70 + super(testName);
14.71 + }
14.72 +
14.73 + public void testBeanFactoryMethods() {
14.74 + Content content = new Content();
14.75 + ArrayList array = new ArrayList();
14.76 + LinkedList linked = new LinkedList();
14.77 + content.addPair(new PairImpl<Object>("array", array));
14.78 + content.addPair(new PairImpl<Object>("linked", linked));
14.79 + content.addPair(new PairImpl<Object>(null, new HashMap()));
14.80 + Lookup lookup = new AbstractLookup(content);
14.81 + ApplicationContext ctx = LookupBasedApplicationContext.create(lookup, this.getClass().getClassLoader());
14.82 +
14.83 + assertSame(array, ctx.getBean("array"));
14.84 +
14.85 + assertSame(array, ctx.getBean("array", ArrayList.class));
14.86 + // Order should be preserved.
14.87 + assertSame(array, ctx.getBean("array", List.class));
14.88 + assertNull(ctx.getBean("array", LinkedList.class));
14.89 +
14.90 + assertSame(array, ctx.getBean("array", new Object[0]));
14.91 + try {
14.92 + ctx.getBean("array", new Object[] { null });
14.93 + fail();
14.94 + } catch (BeanDefinitionStoreException e) {}
14.95 + try {
14.96 + ctx.getBean("foo", new Object[0]);
14.97 + fail();
14.98 + } catch (NoSuchBeanDefinitionException e) {}
14.99 +
14.100 + assertTrue(ctx.containsBean("array"));
14.101 + assertFalse(ctx.containsBean("foo"));
14.102 +
14.103 + assertTrue(ctx.isSingleton("array"));
14.104 + try {
14.105 + ctx.isSingleton("foo");
14.106 + } catch (NoSuchBeanDefinitionException e) {}
14.107 +
14.108 + assertFalse(ctx.isPrototype("array"));
14.109 + try {
14.110 + ctx.isPrototype("foo");
14.111 + } catch (NoSuchBeanDefinitionException e) {}
14.112 +
14.113 + assertTrue(ctx.isTypeMatch("array", ArrayList.class));
14.114 + assertTrue(ctx.isTypeMatch("array", List.class));
14.115 + assertFalse(ctx.isTypeMatch("linked", ArrayList.class));
14.116 + try {
14.117 + ctx.isTypeMatch("foo", Object.class);
14.118 + } catch (NoSuchBeanDefinitionException e) {}
14.119 +
14.120 + assertSame(ArrayList.class, ctx.getType("array"));
14.121 +
14.122 + assertTrue(ctx.containsBeanDefinition("array"));
14.123 + assertFalse(ctx.containsBeanDefinition("foo"));
14.124 +
14.125 + assertEquals(3, ctx.getBeanDefinitionCount());
14.126 +
14.127 + String[] names = ctx.getBeanDefinitionNames();
14.128 + assertEquals(2, names.length);
14.129 + assertEquals("array", names[0]);
14.130 + assertEquals("linked", names[1]);
14.131 +
14.132 + names = ctx.getBeanNamesForType(List.class);
14.133 + assertEquals(2, names.length);
14.134 + assertEquals("array", names[0]);
14.135 + assertEquals("linked", names[1]);
14.136 +
14.137 + Map beans = ctx.getBeansOfType(List.class);
14.138 + assertEquals(2, beans.size());
14.139 + assertSame(array, beans.get("array"));
14.140 + assertSame(linked, beans.get("linked"));
14.141 +
14.142 + assertTrue(ctx.containsLocalBean("array"));
14.143 + assertFalse(ctx.containsLocalBean("foo"));
14.144 + }
14.145 +
14.146 + private static final class PairImpl<T> extends Pair<T> {
14.147 +
14.148 + private final String id;
14.149 + private final T instance;
14.150 +
14.151 + public PairImpl(String id, T instance) {
14.152 + this.id = id;
14.153 + this.instance = instance;
14.154 + }
14.155 +
14.156 + @Override
14.157 + protected boolean instanceOf(Class<?> c) {
14.158 + return c.isInstance(instance);
14.159 + }
14.160 +
14.161 + @Override
14.162 + protected boolean creatorOf(Object obj) {
14.163 + return obj == instance;
14.164 + }
14.165 +
14.166 + @Override
14.167 + public T getInstance() {
14.168 + return instance;
14.169 + }
14.170 +
14.171 + @Override
14.172 + @SuppressWarnings("unchecked")
14.173 + public Class<? extends T> getType() {
14.174 + return (Class<? extends T>)instance.getClass();
14.175 + }
14.176 +
14.177 + @Override
14.178 + public String getId() {
14.179 + return id;
14.180 + }
14.181 +
14.182 + @Override
14.183 + public String getDisplayName() {
14.184 + return getId();
14.185 + }
14.186 + }
14.187 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/spring.lookup/src/test/java/org/netbeans/modules/spring/lookup/TestUtils.java Tue Apr 21 22:17:32 2009 +0200
15.3 @@ -0,0 +1,77 @@
15.4 +/*
15.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
15.6 + *
15.7 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
15.8 + *
15.9 + * The contents of this file are subject to the terms of either the GNU
15.10 + * General Public License Version 2 only ("GPL") or the Common
15.11 + * Development and Distribution License("CDDL") (collectively, the
15.12 + * "License"). You may not use this file except in compliance with the
15.13 + * License. You can obtain a copy of the License at
15.14 + * http://www.netbeans.org/cddl-gplv2.html
15.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
15.16 + * specific language governing permissions and limitations under the
15.17 + * License. When distributing the software, include this License Header
15.18 + * Notice in each file and include the License file at
15.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15.20 + * particular file as subject to the "Classpath" exception as provided
15.21 + * by Sun in the GPL Version 2 section of the License file that
15.22 + * accompanied this code. If applicable, add the following below the
15.23 + * License Header, with the fields enclosed by brackets [] replaced by
15.24 + * your own identifying information:
15.25 + * "Portions Copyrighted [year] [name of copyright owner]"
15.26 + *
15.27 + * Contributor(s):
15.28 + *
15.29 + * The Original Software is NetBeans. The Initial Developer of the Original
15.30 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
15.31 + * Microsystems, Inc. All Rights Reserved.
15.32 + *
15.33 + * If you wish your version of this file to be governed by only the CDDL
15.34 + * or only the GPL Version 2, indicate your decision by adding
15.35 + * "[Contributor] elects to include this software in this distribution
15.36 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
15.37 + * single choice of license, a recipient has the option to distribute
15.38 + * your version of this file under either the CDDL, the GPL Version 2 or
15.39 + * to extend the choice of license to its licensees as provided above.
15.40 + * However, if you add GPL Version 2 code and therefore, elected the GPL
15.41 + * Version 2 license, then the option applies only if the new code is
15.42 + * made subject to such option by the copyright holder.
15.43 + */
15.44 +
15.45 +package org.netbeans.modules.spring.lookup;
15.46 +
15.47 +import java.io.ByteArrayInputStream;
15.48 +import java.io.IOException;
15.49 +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
15.50 +import org.springframework.context.ApplicationContext;
15.51 +import org.springframework.context.support.GenericApplicationContext;
15.52 +import org.xml.sax.InputSource;
15.53 +
15.54 +/**
15.55 + *
15.56 + * @author Andrei Badea
15.57 + */
15.58 +public class TestUtils {
15.59 +
15.60 + public static InputSource createInputSource(String contents) throws IOException {
15.61 + String xml =
15.62 + "<?xml version='1.0' encoding='UTF-8'?>" +
15.63 + "<beans xmlns='http://www.springframework.org/schema/beans'" +
15.64 + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
15.65 + " xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd'>" +
15.66 + contents +
15.67 + "</beans>";
15.68 + return new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
15.69 + }
15.70 +
15.71 + public static ApplicationContext createAppContext(String contents) throws IOException {
15.72 + GenericApplicationContext appContext = new GenericApplicationContext();
15.73 + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
15.74 + reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
15.75 + reader.loadBeanDefinitions(createInputSource(contents));
15.76 + appContext.refresh();
15.77 + return appContext;
15.78 + }
15.79 +}
15.80 +
16.1 --- a/spring.lookup/test/unit/src/org/netbeans/modules/spring/lookup/ApplicationContextBasedLookupTest.java Tue Apr 21 21:41:25 2009 +0200
16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
16.3 @@ -1,155 +0,0 @@
16.4 -/*
16.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
16.6 - *
16.7 - * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
16.8 - *
16.9 - * The contents of this file are subject to the terms of either the GNU
16.10 - * General Public License Version 2 only ("GPL") or the Common
16.11 - * Development and Distribution License("CDDL") (collectively, the
16.12 - * "License"). You may not use this file except in compliance with the
16.13 - * License. You can obtain a copy of the License at
16.14 - * http://www.netbeans.org/cddl-gplv2.html
16.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16.16 - * specific language governing permissions and limitations under the
16.17 - * License. When distributing the software, include this License Header
16.18 - * Notice in each file and include the License file at
16.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
16.20 - * particular file as subject to the "Classpath" exception as provided
16.21 - * by Sun in the GPL Version 2 section of the License file that
16.22 - * accompanied this code. If applicable, add the following below the
16.23 - * License Header, with the fields enclosed by brackets [] replaced by
16.24 - * your own identifying information:
16.25 - * "Portions Copyrighted [year] [name of copyright owner]"
16.26 - *
16.27 - * Contributor(s):
16.28 - *
16.29 - * The Original Software is NetBeans. The Initial Developer of the Original
16.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
16.31 - * Microsystems, Inc. All Rights Reserved.
16.32 - *
16.33 - * If you wish your version of this file to be governed by only the CDDL
16.34 - * or only the GPL Version 2, indicate your decision by adding
16.35 - * "[Contributor] elects to include this software in this distribution
16.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
16.37 - * single choice of license, a recipient has the option to distribute
16.38 - * your version of this file under either the CDDL, the GPL Version 2 or
16.39 - * to extend the choice of license to its licensees as provided above.
16.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
16.41 - * Version 2 license, then the option applies only if the new code is
16.42 - * made subject to such option by the copyright holder.
16.43 - */
16.44 -
16.45 -package org.netbeans.modules.spring.lookup;
16.46 -
16.47 -import java.util.ArrayList;
16.48 -import java.util.HashMap;
16.49 -import java.util.HashSet;
16.50 -import java.util.Iterator;
16.51 -import java.util.LinkedList;
16.52 -import java.util.List;
16.53 -import java.util.Map;
16.54 -import java.util.Set;
16.55 -import junit.framework.TestCase;
16.56 -import org.openide.util.Lookup;
16.57 -import org.openide.util.Lookup.Item;
16.58 -import org.openide.util.Lookup.Result;
16.59 -import org.openide.util.Lookup.Template;
16.60 -import org.springframework.context.ApplicationContext;
16.61 -
16.62 -/**
16.63 - *
16.64 - * @author Andrei Badea
16.65 - */
16.66 -public class ApplicationContextBasedLookupTest extends TestCase {
16.67 -
16.68 - public ApplicationContextBasedLookupTest(String testName) {
16.69 - super(testName);
16.70 - }
16.71 -
16.72 - public void testLookupSingle() throws Exception {
16.73 - ApplicationContext ctx = TestUtils.createAppContext(
16.74 - "<bean id='foo' class='java.util.ArrayList'/>" +
16.75 - "<bean id='baz' class='java.util.HashMap'/>");
16.76 - ArrayList foo = (ArrayList)ctx.getBean("foo");
16.77 - HashMap baz = (HashMap)ctx.getBean("baz");
16.78 - Lookup lookup = new ApplicationContextBasedLookup(ctx);
16.79 -
16.80 - assertSame(foo, lookup.lookup(List.class));
16.81 - assertSame(foo, lookup.lookup(ArrayList.class));
16.82 - assertSame(baz, lookup.lookup(Map.class));
16.83 - // Order should be preserved.
16.84 - assertSame(foo, lookup.lookup(Object.class));
16.85 - }
16.86 -
16.87 - public void testItem() throws Exception {
16.88 - ApplicationContext ctx = TestUtils.createAppContext(
16.89 - "<bean id='foo' class='java.util.ArrayList'/>");
16.90 - ArrayList foo = (ArrayList)ctx.getBean("foo");
16.91 - Lookup lookup = new ApplicationContextBasedLookup(ctx);
16.92 -
16.93 - Item item = lookup.lookupItem(new Template<List>(List.class, null, null));
16.94 - assertEquals("foo", item.getId());
16.95 - assertEquals("foo", item.getDisplayName());
16.96 - assertSame(ArrayList.class, item.getType());
16.97 - assertSame(foo, item.getInstance());
16.98 - }
16.99 -
16.100 - public void testLookupResult() throws Exception {
16.101 - ApplicationContext ctx = TestUtils.createAppContext(
16.102 - "<bean id='foo' class='java.util.ArrayList'/>" +
16.103 - "<bean id='bar' class='java.util.LinkedList'/>" +
16.104 - "<bean id='baz' class='java.util.HashMap'/>");
16.105 - ArrayList foo = (ArrayList)ctx.getBean("foo");
16.106 - LinkedList bar = (LinkedList)ctx.getBean("bar");
16.107 - HashMap baz = (HashMap)ctx.getBean("baz");
16.108 - Lookup lookup = new ApplicationContextBasedLookup(ctx);
16.109 -
16.110 - assertSame(foo, lookup.lookupItem(new Template<List>(List.class, null, foo)).getInstance());
16.111 - assertSame(bar, lookup.lookupItem(new Template<List>(List.class, "bar", bar)).getInstance());
16.112 -
16.113 - assertNull(lookup.lookupItem(new Template<List>(List.class, "none", null)));
16.114 - assertNull(lookup.lookupItem(new Template<List>(List.class, "foo", bar)));
16.115 -
16.116 - Result<List> listResult = lookup.lookupResult(List.class);
16.117 - Iterator<Class<? extends List>> listClassIterator = listResult.allClasses().iterator();
16.118 - assertSame(ArrayList.class, listClassIterator.next());
16.119 - assertSame(LinkedList.class, listClassIterator.next());
16.120 - assertFalse(listClassIterator.hasNext());
16.121 - Iterator<? extends List> listIterator = listResult.allInstances().iterator();
16.122 - assertSame(foo, listIterator.next());
16.123 - assertSame(bar, listIterator.next());
16.124 - assertFalse(listIterator.hasNext());
16.125 - }
16.126 -
16.127 - public void testAliases() throws Exception {
16.128 - ApplicationContext ctx = TestUtils.createAppContext(
16.129 - "<alias name='foo' alias='bar'/>" +
16.130 - "<bean id='foo' class='java.util.ArrayList'/>" +
16.131 - "<bean id='baz' class='java.util.HashMap'/>");
16.132 - ArrayList foo = (ArrayList)ctx.getBean("foo");
16.133 - Lookup lookup = new ApplicationContextBasedLookup(ctx);
16.134 -
16.135 - assertSame(foo, lookup.lookupItem(new Template<List>(List.class, "bar", null)).getInstance());
16.136 -
16.137 - Result<List> result = lookup.lookup(new Template<List>(List.class, null, foo));
16.138 - Iterator<? extends Item<List>> iterator = result.allItems().iterator();
16.139 - Set<String> ids = new HashSet<String>();
16.140 - Item item = iterator.next();
16.141 - assertSame(foo, item.getInstance());
16.142 - ids.add(item.getId());
16.143 - item = iterator.next();
16.144 - assertSame(foo, item.getInstance());
16.145 - ids.add(item.getId());
16.146 - assertEquals(2, ids.size());
16.147 - assertTrue(ids.contains("foo"));
16.148 - assertTrue(ids.contains("bar"));
16.149 - }
16.150 -
16.151 - public void testPrototypesIgnored() throws Exception {
16.152 - ApplicationContext ctx = TestUtils.createAppContext(
16.153 - "<bean id='foo' class='java.util.ArrayList' scope='prototype'/>");
16.154 - Lookup lookup = new ApplicationContextBasedLookup(ctx);
16.155 -
16.156 - assertNull(lookup.lookup(List.class));
16.157 - }
16.158 -}
17.1 --- a/spring.lookup/test/unit/src/org/netbeans/modules/spring/lookup/LookupBasedApplicationContextTest.java Tue Apr 21 21:41:25 2009 +0200
17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
17.3 @@ -1,184 +0,0 @@
17.4 -/*
17.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
17.6 - *
17.7 - * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
17.8 - *
17.9 - * The contents of this file are subject to the terms of either the GNU
17.10 - * General Public License Version 2 only ("GPL") or the Common
17.11 - * Development and Distribution License("CDDL") (collectively, the
17.12 - * "License"). You may not use this file except in compliance with the
17.13 - * License. You can obtain a copy of the License at
17.14 - * http://www.netbeans.org/cddl-gplv2.html
17.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
17.16 - * specific language governing permissions and limitations under the
17.17 - * License. When distributing the software, include this License Header
17.18 - * Notice in each file and include the License file at
17.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17.20 - * particular file as subject to the "Classpath" exception as provided
17.21 - * by Sun in the GPL Version 2 section of the License file that
17.22 - * accompanied this code. If applicable, add the following below the
17.23 - * License Header, with the fields enclosed by brackets [] replaced by
17.24 - * your own identifying information:
17.25 - * "Portions Copyrighted [year] [name of copyright owner]"
17.26 - *
17.27 - * Contributor(s):
17.28 - *
17.29 - * The Original Software is NetBeans. The Initial Developer of the Original
17.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17.31 - * Microsystems, Inc. All Rights Reserved.
17.32 - *
17.33 - * If you wish your version of this file to be governed by only the CDDL
17.34 - * or only the GPL Version 2, indicate your decision by adding
17.35 - * "[Contributor] elects to include this software in this distribution
17.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
17.37 - * single choice of license, a recipient has the option to distribute
17.38 - * your version of this file under either the CDDL, the GPL Version 2 or
17.39 - * to extend the choice of license to its licensees as provided above.
17.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
17.41 - * Version 2 license, then the option applies only if the new code is
17.42 - * made subject to such option by the copyright holder.
17.43 - */
17.44 -
17.45 -package org.netbeans.modules.spring.lookup;
17.46 -
17.47 -import java.util.ArrayList;
17.48 -import java.util.HashMap;
17.49 -import java.util.LinkedList;
17.50 -import java.util.List;
17.51 -import java.util.Map;
17.52 -import junit.framework.TestCase;
17.53 -import org.openide.util.Lookup;
17.54 -import org.openide.util.lookup.AbstractLookup;
17.55 -import org.openide.util.lookup.AbstractLookup.Content;
17.56 -import org.openide.util.lookup.AbstractLookup.Pair;
17.57 -import org.springframework.beans.factory.BeanDefinitionStoreException;
17.58 -import org.springframework.beans.factory.NoSuchBeanDefinitionException;
17.59 -import org.springframework.context.ApplicationContext;
17.60 -
17.61 -/**
17.62 - *
17.63 - * @author Andrei Badea
17.64 - */
17.65 -public class LookupBasedApplicationContextTest extends TestCase {
17.66 -
17.67 - // TODO test others ApplicationContext methods.
17.68 -
17.69 - public LookupBasedApplicationContextTest(String testName) {
17.70 - super(testName);
17.71 - }
17.72 -
17.73 - public void testBeanFactoryMethods() {
17.74 - Content content = new Content();
17.75 - ArrayList array = new ArrayList();
17.76 - LinkedList linked = new LinkedList();
17.77 - content.addPair(new PairImpl<Object>("array", array));
17.78 - content.addPair(new PairImpl<Object>("linked", linked));
17.79 - content.addPair(new PairImpl<Object>(null, new HashMap()));
17.80 - Lookup lookup = new AbstractLookup(content);
17.81 - ApplicationContext ctx = LookupBasedApplicationContext.create(lookup, this.getClass().getClassLoader());
17.82 -
17.83 - assertSame(array, ctx.getBean("array"));
17.84 -
17.85 - assertSame(array, ctx.getBean("array", ArrayList.class));
17.86 - // Order should be preserved.
17.87 - assertSame(array, ctx.getBean("array", List.class));
17.88 - assertNull(ctx.getBean("array", LinkedList.class));
17.89 -
17.90 - assertSame(array, ctx.getBean("array", new Object[0]));
17.91 - try {
17.92 - ctx.getBean("array", new Object[] { null });
17.93 - fail();
17.94 - } catch (BeanDefinitionStoreException e) {}
17.95 - try {
17.96 - ctx.getBean("foo", new Object[0]);
17.97 - fail();
17.98 - } catch (NoSuchBeanDefinitionException e) {}
17.99 -
17.100 - assertTrue(ctx.containsBean("array"));
17.101 - assertFalse(ctx.containsBean("foo"));
17.102 -
17.103 - assertTrue(ctx.isSingleton("array"));
17.104 - try {
17.105 - ctx.isSingleton("foo");
17.106 - } catch (NoSuchBeanDefinitionException e) {}
17.107 -
17.108 - assertFalse(ctx.isPrototype("array"));
17.109 - try {
17.110 - ctx.isPrototype("foo");
17.111 - } catch (NoSuchBeanDefinitionException e) {}
17.112 -
17.113 - assertTrue(ctx.isTypeMatch("array", ArrayList.class));
17.114 - assertTrue(ctx.isTypeMatch("array", List.class));
17.115 - assertFalse(ctx.isTypeMatch("linked", ArrayList.class));
17.116 - try {
17.117 - ctx.isTypeMatch("foo", Object.class);
17.118 - } catch (NoSuchBeanDefinitionException e) {}
17.119 -
17.120 - assertSame(ArrayList.class, ctx.getType("array"));
17.121 -
17.122 - assertTrue(ctx.containsBeanDefinition("array"));
17.123 - assertFalse(ctx.containsBeanDefinition("foo"));
17.124 -
17.125 - assertEquals(3, ctx.getBeanDefinitionCount());
17.126 -
17.127 - String[] names = ctx.getBeanDefinitionNames();
17.128 - assertEquals(2, names.length);
17.129 - assertEquals("array", names[0]);
17.130 - assertEquals("linked", names[1]);
17.131 -
17.132 - names = ctx.getBeanNamesForType(List.class);
17.133 - assertEquals(2, names.length);
17.134 - assertEquals("array", names[0]);
17.135 - assertEquals("linked", names[1]);
17.136 -
17.137 - Map beans = ctx.getBeansOfType(List.class);
17.138 - assertEquals(2, beans.size());
17.139 - assertSame(array, beans.get("array"));
17.140 - assertSame(linked, beans.get("linked"));
17.141 -
17.142 - assertTrue(ctx.containsLocalBean("array"));
17.143 - assertFalse(ctx.containsLocalBean("foo"));
17.144 - }
17.145 -
17.146 - private static final class PairImpl<T> extends Pair<T> {
17.147 -
17.148 - private final String id;
17.149 - private final T instance;
17.150 -
17.151 - public PairImpl(String id, T instance) {
17.152 - this.id = id;
17.153 - this.instance = instance;
17.154 - }
17.155 -
17.156 - @Override
17.157 - protected boolean instanceOf(Class<?> c) {
17.158 - return c.isInstance(instance);
17.159 - }
17.160 -
17.161 - @Override
17.162 - protected boolean creatorOf(Object obj) {
17.163 - return obj == instance;
17.164 - }
17.165 -
17.166 - @Override
17.167 - public T getInstance() {
17.168 - return instance;
17.169 - }
17.170 -
17.171 - @Override
17.172 - @SuppressWarnings("unchecked")
17.173 - public Class<? extends T> getType() {
17.174 - return (Class<? extends T>)instance.getClass();
17.175 - }
17.176 -
17.177 - @Override
17.178 - public String getId() {
17.179 - return id;
17.180 - }
17.181 -
17.182 - @Override
17.183 - public String getDisplayName() {
17.184 - return getId();
17.185 - }
17.186 - }
17.187 -}
18.1 --- a/spring.lookup/test/unit/src/org/netbeans/modules/spring/lookup/TestUtils.java Tue Apr 21 21:41:25 2009 +0200
18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
18.3 @@ -1,77 +0,0 @@
18.4 -/*
18.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
18.6 - *
18.7 - * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
18.8 - *
18.9 - * The contents of this file are subject to the terms of either the GNU
18.10 - * General Public License Version 2 only ("GPL") or the Common
18.11 - * Development and Distribution License("CDDL") (collectively, the
18.12 - * "License"). You may not use this file except in compliance with the
18.13 - * License. You can obtain a copy of the License at
18.14 - * http://www.netbeans.org/cddl-gplv2.html
18.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
18.16 - * specific language governing permissions and limitations under the
18.17 - * License. When distributing the software, include this License Header
18.18 - * Notice in each file and include the License file at
18.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
18.20 - * particular file as subject to the "Classpath" exception as provided
18.21 - * by Sun in the GPL Version 2 section of the License file that
18.22 - * accompanied this code. If applicable, add the following below the
18.23 - * License Header, with the fields enclosed by brackets [] replaced by
18.24 - * your own identifying information:
18.25 - * "Portions Copyrighted [year] [name of copyright owner]"
18.26 - *
18.27 - * Contributor(s):
18.28 - *
18.29 - * The Original Software is NetBeans. The Initial Developer of the Original
18.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
18.31 - * Microsystems, Inc. All Rights Reserved.
18.32 - *
18.33 - * If you wish your version of this file to be governed by only the CDDL
18.34 - * or only the GPL Version 2, indicate your decision by adding
18.35 - * "[Contributor] elects to include this software in this distribution
18.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
18.37 - * single choice of license, a recipient has the option to distribute
18.38 - * your version of this file under either the CDDL, the GPL Version 2 or
18.39 - * to extend the choice of license to its licensees as provided above.
18.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
18.41 - * Version 2 license, then the option applies only if the new code is
18.42 - * made subject to such option by the copyright holder.
18.43 - */
18.44 -
18.45 -package org.netbeans.modules.spring.lookup;
18.46 -
18.47 -import java.io.ByteArrayInputStream;
18.48 -import java.io.IOException;
18.49 -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
18.50 -import org.springframework.context.ApplicationContext;
18.51 -import org.springframework.context.support.GenericApplicationContext;
18.52 -import org.xml.sax.InputSource;
18.53 -
18.54 -/**
18.55 - *
18.56 - * @author Andrei Badea
18.57 - */
18.58 -public class TestUtils {
18.59 -
18.60 - public static InputSource createInputSource(String contents) throws IOException {
18.61 - String xml =
18.62 - "<?xml version='1.0' encoding='UTF-8'?>" +
18.63 - "<beans xmlns='http://www.springframework.org/schema/beans'" +
18.64 - " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
18.65 - " xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd'>" +
18.66 - contents +
18.67 - "</beans>";
18.68 - return new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
18.69 - }
18.70 -
18.71 - public static ApplicationContext createAppContext(String contents) throws IOException {
18.72 - GenericApplicationContext appContext = new GenericApplicationContext();
18.73 - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
18.74 - reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
18.75 - reader.loadBeanDefinitions(createInputSource(contents));
18.76 - appContext.refresh();
18.77 - return appContext;
18.78 - }
18.79 -}
18.80 -