blob: 51228168b84112ef3e01a163a58b45cfa33abf34 [file] [log] [blame]
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +00001/*
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.cm.impl;
20
21
22import java.io.IOException;
23import java.util.Dictionary;
24import java.util.HashSet;
25import java.util.Hashtable;
26import java.util.Set;
27
28import org.apache.felix.cm.PersistenceManager;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000029
30
31/**
Felix Meschbergere2ca8332007-06-06 06:27:31 +000032 * The <code>Factory</code> class is used to manage mappings between factory
33 * PIDs the configuration PID belonging to it.
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000034 */
Felix Meschbergeref470042009-08-19 05:52:41 +000035class Factory extends ConfigurationBase
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000036{
37
Felix Meschbergere2ca8332007-06-06 06:27:31 +000038 public static final String FACTORY_PID = "factory.pid";
39
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000040 public static final String FACTORY_PID_LIST = "factory.pidList";
41
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000042 // the set of configuration PIDs belonging to this factory
Felix Meschbergeref470042009-08-19 05:52:41 +000043 private final Set pids = new HashSet();;
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000044
45
46 static boolean exists( PersistenceManager persistenceManager, String factoryPid )
47 {
48 return persistenceManager.exists( factoryPidToIdentifier( factoryPid ) );
49 }
50
51
Felix Meschberger3f9e4da2009-08-17 07:52:39 +000052 static Factory load( ConfigurationManager configurationManager, PersistenceManager persistenceManager,
53 String factoryPid ) throws IOException
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000054 {
55 Dictionary dict = persistenceManager.load( factoryPidToIdentifier( factoryPid ) );
Felix Meschberger3f9e4da2009-08-17 07:52:39 +000056 return new Factory( configurationManager, persistenceManager, factoryPid, dict );
Felix Meschbergere2ca8332007-06-06 06:27:31 +000057 }
58
59
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000060 private static String factoryPidToIdentifier( String factoryPid )
61 {
62 return factoryPid + ".factory";
63 }
64
65
Felix Meschberger3f9e4da2009-08-17 07:52:39 +000066 Factory( ConfigurationManager configurationManager, PersistenceManager persistenceManager, String factoryPid )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000067 {
Felix Meschberger007c50e2011-10-20 12:39:38 +000068 super( configurationManager, persistenceManager, factoryPid );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000069 }
70
71
Felix Meschberger007c50e2011-10-20 12:39:38 +000072 Factory( ConfigurationManager configurationManager, PersistenceManager persistenceManager, String factoryPid,
73 Dictionary props )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000074 {
Felix Meschberger007c50e2011-10-20 12:39:38 +000075 super( configurationManager, persistenceManager, factoryPid );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000076
77 // set pids
78 String[] pidList = ( String[] ) props.get( FACTORY_PID_LIST );
79 if ( pidList != null )
80 {
81 for ( int i = 0; i < pidList.length; i++ )
82 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +000083 pids.add( pidList[i] );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000084 }
85 }
86 }
87
88
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000089 String getFactoryPid()
90 {
Felix Meschbergeref470042009-08-19 05:52:41 +000091 return getBaseId();
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000092 }
93
94
95 Set getPIDs()
96 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +000097 return new HashSet( pids );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +000098 }
99
100
101 boolean addPID( String pid )
102 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +0000103 return pids.add( pid );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000104 }
105
106
107 boolean removePID( String pid )
108 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +0000109 return pids.remove( pid );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000110 }
111
112
113 void store() throws IOException
114 {
115 Hashtable props = new Hashtable();
116
Carsten Ziegelereb80d512007-08-15 11:17:41 +0000117 if ( !pids.isEmpty() )
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000118 {
Carsten Ziegelereb80d512007-08-15 11:17:41 +0000119 props.put( FACTORY_PID_LIST, pids.toArray( new String[pids.size()] ) );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000120 }
121
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000122 String id = factoryPidToIdentifier( this.getFactoryPid() );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000123 if ( props.isEmpty() )
124 {
Felix Meschbergeref470042009-08-19 05:52:41 +0000125 getPersistenceManager().delete( id );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000126 }
127 else
128 {
Carsten Ziegeler6c6f25b2007-08-15 10:04:02 +0000129 props.put( FACTORY_PID, this.getFactoryPid() );
Felix Meschbergeref470042009-08-19 05:52:41 +0000130 getPersistenceManager().store( id, props );
Felix Meschbergeradd2b4a2007-04-11 18:12:33 +0000131 }
132 }
133}