Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cpman.impl; |
| 17 | |
| 18 | import org.onosproject.cpman.SystemInfo; |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | /** |
| 24 | * A factory class which instantiates a system info object. |
| 25 | */ |
| 26 | public final class SystemInfoFactory { |
| 27 | |
| 28 | private final Logger log = getLogger(getClass()); |
| 29 | |
| 30 | private SystemInfo systemInfo; |
| 31 | |
| 32 | // non-instantiable (except for our Singleton) |
| 33 | private SystemInfoFactory() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns system information. |
| 38 | * |
| 39 | * @return reference object of system info |
| 40 | */ |
| 41 | public SystemInfo getSystemInfo() { |
| 42 | synchronized (systemInfo) { |
| 43 | return this.systemInfo; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set system information only if it is empty. |
| 49 | * |
| 50 | * @param systemInfo reference object of system info |
| 51 | */ |
| 52 | public void setSystemInfo(SystemInfo systemInfo) { |
| 53 | synchronized (systemInfo) { |
| 54 | if (this.systemInfo == null) { |
| 55 | this.systemInfo = systemInfo; |
| 56 | } else { |
| 57 | log.warn("System information has already been set"); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns an instance of system info factory. |
| 64 | * |
| 65 | * @return instance of system info factory |
| 66 | */ |
| 67 | public static SystemInfoFactory getInstance() { |
| 68 | return SingletonHelper.INSTANCE; |
| 69 | } |
| 70 | |
| 71 | private static class SingletonHelper { |
| 72 | private static final SystemInfoFactory INSTANCE = new SystemInfoFactory(); |
| 73 | } |
| 74 | } |