KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > ApplicationContextFactory


1 /*
2  * Copyright 28/03/2005 - Vicinity - www.vicinity.com.br All rights reserveds
3  */

4 package org.javabb.infra;
5
6
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.springframework.context.ApplicationContext;
14 import org.springframework.context.support.ClassPathXmlApplicationContext;
15
16
17 /**
18  * This class is used to abstract Spring ApplicationContext creation. Here you
19  * can put a lot of locations based on classpath. Course, for web applications
20  * use Spring support instead it.
21  *
22  * @author Marcos Silva Pereira - marcos.pereira@vicinity.com.br
23  * @since 08/07/2004
24  * @version $Id$
25  */

26 public final class ApplicationContextFactory {
27
28     private static Log log = LogFactory.getLog(ApplicationContextFactory.class);
29
30     private boolean init;
31
32     private List JavaDoc locations = new ArrayList JavaDoc();
33
34     /**
35      * @param location
36      * @return
37      */

38     public ApplicationContextFactory addLocation( String JavaDoc location ) {
39
40         if ((location != null) && !locations.contains(location)) {
41
42             locations.add(location);
43
44         }
45
46         return this;
47
48     }
49
50     /**
51      * @param location
52      * @return
53      */

54     public ApplicationContextFactory addLocation( File JavaDoc location ) {
55
56         return addLocation(location.getPath());
57
58     }
59
60     /**
61      * @param location
62      * @return
63      */

64     public boolean containsLocation( File JavaDoc location ) {
65
66         return containsLocation(location.getPath());
67     }
68
69     /**
70      * @param path
71      * @return
72      */

73     public boolean containsLocation( String JavaDoc path ) {
74
75         return locations.contains(path);
76
77     }
78
79     /**
80      * @return
81      */

82     public ApplicationContext init() {
83
84         if (init) {
85
86             String JavaDoc msg = "ApplicationContextFactory already initialized.";
87
88             log.debug(msg);
89             throw new RuntimeException JavaDoc(msg);
90
91         }
92
93         String JavaDoc[] locals = new String JavaDoc[locations.size()];
94
95         locations.toArray(locals);
96
97         ApplicationContext context;
98         context = new ClassPathXmlApplicationContext(locals);
99
100         init = true;
101
102         return context;
103
104     }
105
106 }
107
Popular Tags