Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * 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 | */ |
| 19 | package org.apache.felix.cm.impl; |
| 20 | |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.Dictionary; |
| 24 | import org.apache.felix.cm.PersistenceManager; |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 25 | import org.osgi.service.log.LogService; |
| 26 | |
| 27 | |
| 28 | abstract class ConfigurationBase |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * The {@link ConfigurationManager configuration manager} instance which |
| 33 | * caused this configuration object to be created. |
| 34 | */ |
| 35 | private final ConfigurationManager configurationManager; |
| 36 | |
| 37 | // the persistence manager storing this factory mapping |
| 38 | private final PersistenceManager persistenceManager; |
| 39 | |
| 40 | // the basic ID of this instance |
| 41 | private final String baseId; |
| 42 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 43 | protected ConfigurationBase( final ConfigurationManager configurationManager, |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 44 | final PersistenceManager persistenceManager, final String baseId ) |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 45 | { |
| 46 | if ( configurationManager == null ) |
| 47 | { |
| 48 | throw new IllegalArgumentException( "ConfigurationManager must not be null" ); |
| 49 | } |
| 50 | |
| 51 | if ( persistenceManager == null ) |
| 52 | { |
| 53 | throw new IllegalArgumentException( "PersistenceManager must not be null" ); |
| 54 | } |
| 55 | |
| 56 | this.configurationManager = configurationManager; |
| 57 | this.persistenceManager = persistenceManager; |
| 58 | this.baseId = baseId; |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | ConfigurationManager getConfigurationManager() |
| 63 | { |
| 64 | return configurationManager; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | PersistenceManager getPersistenceManager() |
| 69 | { |
| 70 | return persistenceManager; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | String getBaseId() |
| 75 | { |
| 76 | return baseId; |
| 77 | } |
| 78 | |
| 79 | |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 80 | abstract void store() throws IOException; |
| 81 | |
| 82 | |
| 83 | void storeSilently() |
| 84 | { |
| 85 | try |
| 86 | { |
| 87 | this.store(); |
| 88 | } |
| 89 | catch ( IOException ioe ) |
| 90 | { |
Felix Meschberger | 4f26929 | 2011-10-21 13:52:31 +0000 | [diff] [blame^] | 91 | configurationManager.log( LogService.LOG_ERROR, "Persisting ID {0} failed", new Object[] |
| 92 | { getBaseId(), ioe } ); |
Felix Meschberger | ef47004 | 2009-08-19 05:52:41 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static protected void replaceProperty( Dictionary properties, String key, String value ) |
| 98 | { |
| 99 | if ( value == null ) |
| 100 | { |
| 101 | properties.remove( key ); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | properties.put( key, value ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |