EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hivegui.application]

COVERAGE SUMMARY FOR SOURCE FILE [PreferencesManagerImpl.java]

nameclass, %method, %block, %line, %
PreferencesManagerImpl.java0%   (0/1)0%   (0/7)0%   (0/284)0%   (0/62)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PreferencesManagerImpl0%   (0/1)0%   (0/7)0%   (0/284)0%   (0/62)
PreferencesManagerImpl (Log, Map, Map, boolean): void 0%   (0/1)0%   (0/46)0%   (0/12)
load (PreferenceContribution): Object 0%   (0/1)0%   (0/58)0%   (0/11)
read (String): Object 0%   (0/1)0%   (0/73)0%   (0/15)
registryDidShutdown (): void 0%   (0/1)0%   (0/21)0%   (0/6)
setContextHolder (ApplicationContextHolder): void 0%   (0/1)0%   (0/6)0%   (0/2)
store (PreferenceContribution, Object): void 0%   (0/1)0%   (0/29)0%   (0/5)
write (String): void 0%   (0/1)0%   (0/51)0%   (0/11)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
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 implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package net.sourceforge.hivegui.application;
16 
17import java.beans.PersistenceDelegate;
18import java.beans.XMLEncoder;
19import java.io.IOException;
20import java.util.HashMap;
21import java.util.Map;
22 
23import org.apache.commons.logging.Log;
24import org.apache.hivemind.events.RegistryShutdownListener;
25import org.jdesktop.application.LocalStorage;
26 
27import net.sourceforge.hiveutils.service.PreferencesManager;
28import net.sourceforge.hiveutils.service.impl.PreferenceContribution;
29 
30/**
31 * @author Jean-Francois Poilpret
32 */
33public class PreferencesManagerImpl
34        implements PreferencesManager, RegistryShutdownListener
35{
36        public PreferencesManagerImpl(        Log                                                                        logger, 
37                                                                        Map<String, PreferenceContribution>        prefsDef, 
38                                                                        Map<Class, PersistenceDelegate>                mappers, 
39                                                                        boolean                                                                autoWrite)
40        {
41                _logger = logger;
42                _autoWrite = autoWrite;
43                _prefsDef = prefsDef;
44                // Initialize XMLEncoder PersistenceDelegates
45                XMLEncoder encoder = null;
46                for (Map.Entry<Class, PersistenceDelegate> entry: mappers.entrySet())
47                {
48                        if (encoder == null)
49                        {
50                                encoder = new XMLEncoder(System.out);
51                        }
52                        encoder.setPersistenceDelegate(entry.getKey(), entry.getValue());
53                }
54        }
55        
56        public void        setContextHolder(ApplicationContextHolder holder)
57        {
58                _storage = holder.getContext().getLocalStorage();
59        }
60 
61        public void registryDidShutdown()
62        {
63                if (!_autoWrite)
64                {
65                        return;
66                }
67                // Save all loaded preference beans back to the preferences store
68                for (String pref: _prefs.keySet())
69                {
70                        write(pref);
71                }
72        }
73        
74        public Object        read(String name)
75        {
76                // First check if preference is already read and cached
77                Object pref;
78                synchronized (this)
79                {
80                        pref = _prefs.get(name);
81                }
82                if (pref != null)
83                {
84                        return pref;
85                }
86                // Not read yet, get configuration for this preference
87                PreferenceContribution def = _prefsDef.get(name);
88                if (def == null)
89                {
90                        _logger.warn("read(): '" + name + "' does not exist.");
91                        return null;
92                }
93                // Instantiate this preference from the actual prefs repository (AppFW)
94                pref = load(def);
95                synchronized (this)
96                {
97                        if (_prefs.get(name) == null)
98                        {
99                                _prefs.put(name, pref);
100                        }
101                }
102                return pref;
103        }
104        
105        protected Object        load(PreferenceContribution def)
106        {
107                Object prefs = null;
108                try
109                {
110                        prefs = _storage.load(def.getName() + ".xml");
111                }
112                catch (IOException e)
113                {
114                        _logger.warn("load(): '" + def.getName() + "' could not be loaded", e);
115                }
116                if (prefs != null)
117                {
118                        return prefs;
119                }
120                // CSOFF: IllegalCatchCheck
121                try
122                {
123                        // Instantiate a new default value for this class
124                        return def.getBeanClass().newInstance();
125                }
126                catch (Exception e)
127                {
128                        _logger.error("load(): '" + def.getName() + "' has failed.", e);
129                        return null;
130                }
131                // CSON: IllegalCatchCheck
132        }
133        
134        @SuppressWarnings("unchecked")
135        public void                write(String name)
136        {
137                // First check that preference is already read and cached
138                Object pref;
139                synchronized (this)
140                {
141                        pref = _prefs.get(name);
142                }
143                if (pref == null)
144                {
145                        _logger.warn("write(): '" + name + "' is not in cache.");
146                        return;
147                }
148                PreferenceContribution def = _prefsDef.get(name);
149                store(def, pref);
150                if (def.getEventChannel() != null)
151                {
152                        def.getEventChannel().push(pref);
153                }
154        }
155        
156        protected void        store(PreferenceContribution def, Object pref)
157        {
158                try
159                {
160                        _storage.save(pref, def.getName() + ".xml");
161                }
162                catch (IOException e)
163                {
164                        _logger.error("store():" + def.getName(), e);
165                }
166        }
167 
168        private final Log                                                                        _logger;
169        private final Map<String, PreferenceContribution>        _prefsDef;
170        private final Map<String, Object>                                        _prefs = new HashMap<String, Object>();
171        private final boolean                                                                _autoWrite;
172        private LocalStorage                                                                _storage;
173}

[all classes][net.sourceforge.hivegui.application]
EMMA 2.0.5312 (C) Vladimir Roubtsov