Carsten Ziegeler | 122a404 | 2007-07-30 12:57:54 +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.sandbox.preferences.impl; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import org.apache.felix.sandbox.preferences.BackingStoreManager; |
| 28 | import org.apache.felix.sandbox.preferences.PreferencesDescription; |
| 29 | import org.apache.felix.sandbox.preferences.PreferencesImpl; |
| 30 | import org.osgi.service.prefs.BackingStoreException; |
| 31 | import org.osgi.service.prefs.Preferences; |
| 32 | import org.osgi.service.prefs.PreferencesService; |
| 33 | |
| 34 | /** |
| 35 | * This is an implementation of the OSGI Preferences Service, Version 1.1. |
| 36 | */ |
| 37 | public class PreferencesServiceImpl implements PreferencesService { |
| 38 | |
| 39 | /** This is the system preferences tree. */ |
| 40 | protected PreferencesImpl systemTree; |
| 41 | |
| 42 | /** This is the map containing the user preferences trees. */ |
| 43 | protected final Map trees = new HashMap(); |
| 44 | |
| 45 | /** The service id for the bundle this service belongs to. */ |
| 46 | protected final Long bundleId; |
| 47 | |
| 48 | /** The backing store manager. */ |
| 49 | protected final BackingStoreManager storeManager; |
| 50 | |
| 51 | public PreferencesServiceImpl(Long id, |
| 52 | BackingStoreManager storeManager) { |
| 53 | this.bundleId = id; |
| 54 | this.storeManager = storeManager; |
| 55 | try { |
| 56 | // load prefs first |
| 57 | PreferencesImpl[] prefs = null; |
| 58 | prefs = this.storeManager.getStore().loadAll(storeManager, this.bundleId); |
| 59 | for(int i=0;i<prefs.length;i++) { |
| 60 | if ( prefs[i].getDescription().getIdentifier() == null ) { |
| 61 | this.systemTree = prefs[i]; |
| 62 | } else { |
| 63 | this.trees.put(prefs[i].getDescription().getIdentifier(), prefs); |
| 64 | } |
| 65 | } |
| 66 | } catch (BackingStoreException e) { |
| 67 | // we ignore this here |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @see org.osgi.service.prefs.PreferencesService#getSystemPreferences() |
| 73 | */ |
| 74 | public synchronized Preferences getSystemPreferences() { |
| 75 | if ( this.systemTree == null ) { |
| 76 | this.systemTree = new PreferencesImpl(new PreferencesDescription(this.bundleId, null), this.storeManager); |
| 77 | } |
| 78 | // sync with latest version from store |
| 79 | try { |
| 80 | this.systemTree.sync(); |
| 81 | } catch (BackingStoreException ignore) { |
| 82 | // we ignore this |
| 83 | } |
| 84 | return this.systemTree; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @see org.osgi.service.prefs.PreferencesService#getUserPreferences(java.lang.String) |
| 89 | */ |
| 90 | public synchronized Preferences getUserPreferences(String name) { |
| 91 | PreferencesImpl result = (PreferencesImpl) this.trees.get(name); |
| 92 | // if the tree does not exist yet, create it |
| 93 | if (result == null) { |
| 94 | result = new PreferencesImpl(new PreferencesDescription(this.bundleId, name), this.storeManager); |
| 95 | this.trees.put(name, result); |
| 96 | } |
| 97 | // sync with latest version from store |
| 98 | try { |
| 99 | result.sync(); |
| 100 | } catch (BackingStoreException ignore) { |
| 101 | // we ignore this |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @see org.osgi.service.prefs.PreferencesService#getUsers() |
| 108 | */ |
| 109 | public synchronized String[] getUsers() { |
| 110 | // TODO - we have to sync with the store |
| 111 | final Set userKeys = this.trees.keySet(); |
| 112 | return (String[])userKeys.toArray(new String[userKeys.size()]); |
| 113 | } |
| 114 | |
| 115 | protected List getAllPreferences() { |
| 116 | final List list = new ArrayList(); |
| 117 | if ( this.systemTree != null ) { |
| 118 | list.add(this.systemTree); |
| 119 | } |
| 120 | list.addAll(this.trees.values()); |
| 121 | return list; |
| 122 | } |
| 123 | } |