View Javadoc

1   package org.apache.maven.continuum.notification.manager.spring;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.continuum.notification.Notifier;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.beans.BeansException;
26  import org.springframework.beans.factory.BeanFactoryUtils;
27  import org.springframework.beans.factory.BeanInitializationException;
28  import org.springframework.beans.factory.FactoryBean;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.context.ApplicationContextAware;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /**
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   * @version $Id: NotifierFactoryBean.java 1372260 2012-08-13 04:29:09Z brett $
38   */
39  public class NotifierFactoryBean
40      implements FactoryBean, ApplicationContextAware
41  {
42      private static final Logger log = LoggerFactory.getLogger( NotifierFactoryBean.class );
43  
44      private ApplicationContext applicationContext;
45  
46      public Object getObject()
47          throws Exception
48      {
49          Map<String, Notifier> notifiers = new HashMap<String, Notifier>();
50  
51          Map<String, Notifier> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors( applicationContext,
52                                                                                        Notifier.class );
53  
54          for ( Notifier notifier : beans.values() )
55          {
56  
57              if ( notifiers.containsKey( notifier.getType() ) )
58              {
59                  throw new BeanInitializationException(
60                      "There are two Notifier beans in the appllication context for Notifier type " + notifier.getType() +
61                          ". Probably two conflicting scm implementations are present in the classpath." );
62              }
63  
64              if ( log.isDebugEnabled() )
65              {
66                  log.debug(
67                      "put provider with type " + notifier.getType() + " and class " + notifier.getClass().getName() );
68              }
69              notifiers.put( notifier.getType(), notifier );
70          }
71          return notifiers;
72      }
73  
74      public Class getObjectType()
75      {
76          return Map.class;
77      }
78  
79      public boolean isSingleton()
80      {
81          return true;
82      }
83  
84      public void setApplicationContext( ApplicationContext applicationContext )
85          throws BeansException
86      {
87          this.applicationContext = applicationContext;
88      }
89  }