KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > impl > DefaultFactory


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository.impl;
19
20
21 import java.io.File JavaDoc;
22
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.repository.Artifact;
26 import org.apache.avalon.repository.RepositoryRuntimeException;
27 import org.apache.avalon.repository.provider.RepositoryCriteria;
28 import org.apache.avalon.repository.provider.InitialContext;
29 import org.apache.avalon.repository.provider.Factory;
30
31 import org.apache.avalon.util.i18n.ResourceManager;
32 import org.apache.avalon.util.i18n.Resources;
33
34 /**
35  * The default repository factory implementation.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version $Revision: 1.10 $
39  */

40 public class DefaultFactory implements Factory
41 {
42     //--------------------------------------------------------------------------
43
// static
44
//--------------------------------------------------------------------------
45

46     private static Resources REZ =
47         ResourceManager.getPackageResources( DefaultFactory.class );
48
49     private String JavaDoc[] m_hosts;
50
51     //--------------------------------------------------------------------------
52
// state
53
//--------------------------------------------------------------------------
54

55     private final InitialContext m_context;
56
57     //--------------------------------------------------------------------------
58
// constructor
59
//--------------------------------------------------------------------------
60

61    /**
62     * Creation of a new default repository manager factory.
63     * @param context the initial context
64     * @exception NullPointerException if the supplied context is null
65     */

66     public DefaultFactory( InitialContext context )
67     {
68         if( null == context )
69           throw new NullPointerException JavaDoc( "context" );
70         m_context = context;
71     }
72
73     //--------------------------------------------------------------------------
74
// Factory
75
//--------------------------------------------------------------------------
76

77    /**
78     * Create a new instance of the default criteria.
79     * @return a new default criteria instance
80     */

81     public Map JavaDoc createDefaultCriteria()
82     {
83         try
84         {
85             return new DefaultRepositoryCriteria( m_context );
86         }
87         catch( Throwable JavaDoc e )
88         {
89             final String JavaDoc error =
90               "Could not create default factory criteria.";
91             throw new RepositoryRuntimeException( error, e );
92         }
93     }
94
95    /**
96     * Create a new instance of a repository administrator
97     * using the default parameters.
98     * @return the application instance
99     * @exception Exception if a repository creation error occurs
100     */

101     public Object JavaDoc create() throws Exception JavaDoc
102     {
103         return create( createDefaultCriteria() );
104     }
105
106    /**
107     * Create a new instance of a repository
108     * using the supplied parameters.
109     *
110     * @param map a map of repository parameters
111     * @return the repository
112     */

113     public Object JavaDoc create( Map JavaDoc map ) throws Exception JavaDoc
114     {
115         if( null == map )
116         {
117             throw new NullPointerException JavaDoc( "map" );
118         }
119
120         File JavaDoc root = getCache( map );
121         String JavaDoc[] hosts = getHosts( map );
122         boolean online = getOnlineMode( map );
123         Artifact[] candidates = getFactoryArtifacts( map );
124         return new DefaultRepository( root, hosts, online, candidates );
125     }
126
127     private boolean getOnlineMode( Map JavaDoc map )
128     {
129         Boolean JavaDoc value = (Boolean JavaDoc) map.get(
130             RepositoryCriteria.REPOSITORY_ONLINE_MODE );
131         if( null != value ) return value.booleanValue();
132         return true;
133     }
134
135     private File JavaDoc getCache( Map JavaDoc map )
136     {
137         return (File JavaDoc) map.get(
138             RepositoryCriteria.REPOSITORY_CACHE_DIR );
139     }
140
141     private String JavaDoc[] getHosts( Map JavaDoc map )
142     {
143         return (String JavaDoc[]) map.get(
144             RepositoryCriteria.REPOSITORY_REMOTE_HOSTS );
145     }
146
147     private Artifact[] getFactoryArtifacts( Map JavaDoc map )
148     {
149         return (Artifact[]) map.get(
150             RepositoryCriteria.REPOSITORY_FACTORY_ARTIFACTS );
151     }
152 }
153
Popular Tags