2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.utils;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.Date;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
29 import java.util.Vector;
31 import oic.simulator.serviceprovider.Activator;
32 import oic.simulator.serviceprovider.model.Resource;
33 import oic.simulator.serviceprovider.model.SingleResource;
35 import org.oic.simulator.AttributeValue;
36 import org.oic.simulator.AttributeValue.TypeInfo;
37 import org.oic.simulator.AttributeValue.ValueType;
38 import org.oic.simulator.ILogger.Level;
39 import org.oic.simulator.SimulatorException;
42 * This class has common utility methods.
44 public class Utility {
46 public static String getAutomationStatus(boolean status) {
48 return Constants.ENABLED;
50 return Constants.DISABLED;
54 public static String getAutomationString(boolean status) {
56 return Constants.ENABLE;
58 return Constants.DISABLE;
62 public static boolean getAutomationBoolean(String status) {
64 if (status.equals(Constants.ENABLE)) {
71 public static int getUpdateIntervalFromString(String value) {
72 int result = Constants.DEFAULT_AUTOMATION_INTERVAL;
75 result = Integer.parseInt(value);
76 } catch (NumberFormatException e) {
80 .log(Level.ERROR.ordinal(),
82 getSimulatorErrorString(
84 "Update interval convertion failed."
85 + "Taking the default value("
86 + Constants.DEFAULT_AUTOMATION_INTERVAL
93 public static List<String> convertSetToList(Set<String> typeSet) {
94 if (null == typeSet) {
97 List<String> list = new ArrayList<String>();
98 Iterator<String> typeItr = typeSet.iterator();
99 while (typeItr.hasNext()) {
100 list.add(typeItr.next());
105 public static List<SingleResource> getSingleResourceListFromSet(
106 Set<SingleResource> resources) {
107 if (null == resources) {
110 List<SingleResource> list = new ArrayList<SingleResource>();
111 Iterator<SingleResource> typeItr = resources.iterator();
112 while (typeItr.hasNext()) {
113 list.add(typeItr.next());
118 public static Set<String> convertVectorToSet(Vector<String> vector) {
119 if (null == vector || vector.isEmpty()) {
122 Set<String> resultSet = new HashSet<String>();
123 Enumeration<String> e = vector.elements();
124 while (e.hasMoreElements()) {
125 resultSet.add(e.nextElement().toString());
130 public static String getSimulatorErrorString(Exception e, String info) {
135 if (e instanceof SimulatorException) {
136 SimulatorException simEx = (SimulatorException) e;
137 detail = simEx.message() + "\n";
138 detail += "Exception Type: " + simEx.getClass().getSimpleName()
140 detail += "Error code: " + simEx.code().toString();
142 if (null != info && !info.isEmpty())
143 detail = info + "\n";
145 detail = "Description not available\n";
146 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
147 String msg = e.getMessage();
148 if (null != msg && !msg.isEmpty()) {
149 detail += "Message: " + e.getMessage();
155 public static Set<String> getAttributeTypes() {
156 Set<String> attTypes = new HashSet<String>();
157 ValueType[] types = ValueType.values();
159 attTypes.add(Constants.INT);
160 attTypes.add(Constants.DOUBLE);
161 attTypes.add(Constants.BOOL);
162 attTypes.add(Constants.STRING);
167 public static ValueType getAttributeTypeEnum(String type) {
168 if (null != type && type.trim().length() > 0) {
169 if (type.equalsIgnoreCase(Constants.INT)) {
170 return ValueType.INTEGER;
172 if (type.equalsIgnoreCase(Constants.DOUBLE)) {
173 return ValueType.DOUBLE;
175 if (type.equalsIgnoreCase(Constants.BOOL)) {
176 return ValueType.BOOLEAN;
178 if (type.equalsIgnoreCase(Constants.STRING)) {
179 return ValueType.STRING;
182 return ValueType.UNKNOWN;
185 public static int[] convertSetToArrayInt(Set<Object> allowedValues) {
186 if (null == allowedValues || allowedValues.size() < 1) {
189 int[] arr = new int[allowedValues.size()];
190 Iterator<Object> itr = allowedValues.iterator();
193 while (itr.hasNext()) {
194 arr[i++] = (int) itr.next();
196 } catch (Exception e) {
202 public static double[] convertSetToArrayDouble(Set<Object> allowedValues) {
203 if (null == allowedValues || allowedValues.size() < 1) {
206 double[] arr = new double[allowedValues.size()];
207 Iterator<Object> itr = allowedValues.iterator();
210 while (itr.hasNext()) {
211 arr[i++] = (double) itr.next();
213 } catch (Exception e) {
219 public static boolean[] convertSetToArrayBoolean(Set<Object> allowedValues) {
220 if (null == allowedValues || allowedValues.size() < 1) {
223 boolean[] arr = new boolean[allowedValues.size()];
224 Iterator<Object> itr = allowedValues.iterator();
227 while (itr.hasNext()) {
228 arr[i++] = (boolean) itr.next();
230 } catch (Exception e) {
236 public static String[] convertSetToArrayString(Set<Object> allowedValues) {
237 if (null == allowedValues || allowedValues.size() < 1) {
240 String[] arr = new String[allowedValues.size()];
241 Iterator<Object> itr = allowedValues.iterator();
244 while (itr.hasNext()) {
245 arr[i++] = (String) itr.next();
247 } catch (Exception e) {
253 public static Vector<Integer> convertSetToVectorInt(
254 Set<Object> allowedValues) {
255 if (null == allowedValues || allowedValues.size() < 1) {
258 Vector<Integer> resultVec = new Vector<Integer>();
259 Iterator<Object> itr = allowedValues.iterator();
261 while (itr.hasNext()) {
262 resultVec.add((Integer) itr.next());
264 } catch (Exception e) {
270 public static Vector<Double> convertSetToVectorDouble(
271 Set<Object> allowedValues) {
272 if (null == allowedValues || allowedValues.size() < 1) {
275 Vector<Double> resultVec = new Vector<Double>();
276 Iterator<Object> itr = allowedValues.iterator();
278 while (itr.hasNext()) {
279 resultVec.add((Double) itr.next());
281 } catch (Exception e) {
287 public static Vector<String> convertSetToVectorString(
288 Set<String> allowedValues) {
289 if (null == allowedValues || allowedValues.size() < 1) {
292 Vector<String> resultVec = new Vector<String>();
293 Iterator<String> itr = allowedValues.iterator();
295 while (itr.hasNext()) {
296 resultVec.add((String) itr.next());
298 } catch (Exception e) {
304 public static Set<Object> convertSetStringToSetObject(Set<String> values,
306 if (null == values || values.isEmpty()) {
309 Set<Object> resultSet = new HashSet<Object>();
310 if (AttributeValue.ValueType.INTEGER == type) {
312 Iterator<String> itr = values.iterator();
313 while (itr.hasNext()) {
315 val = Integer.parseInt(itr.next());
317 } catch (NumberFormatException nfe) {
318 // Added for safety. Nothing to do.
321 } else if (AttributeValue.ValueType.DOUBLE == type) {
323 Iterator<String> itr = values.iterator();
324 while (itr.hasNext()) {
326 val = Double.parseDouble(itr.next());
328 } catch (NumberFormatException nfe) {
329 // Added for safety. Nothing to do.
332 } else if (AttributeValue.ValueType.BOOLEAN == type) {
334 resultSet.add(false);
335 } else if (AttributeValue.ValueType.STRING == type) {
336 Iterator<String> itr = values.iterator();
337 while (itr.hasNext()) {
338 resultSet.add(itr.next());
344 public static List<Resource> convertSingleTypeResourceListToBaseType(
345 List<SingleResource> resources) {
346 if (null == resources || resources.isEmpty()) {
349 List<Resource> resultSet = new ArrayList<Resource>();
350 Iterator<SingleResource> itr = resources.iterator();
351 while (itr.hasNext()) {
352 resultSet.add(itr.next());
357 public static String[] convertListToStringArray(List<String> valueList) {
359 if (null != valueList && valueList.size() > 0) {
360 strArr = valueList.toArray(new String[1]);
362 strArr = new String[1];
367 public static Comparator<Resource> resourceComparator = new Comparator<Resource>() {
386 .equalsIgnoreCase(s2Part)) {
387 return extractInt(s1)
413 public static Comparator<SingleResource> singleResourceComparator = new Comparator<SingleResource>() {
416 SingleResource res2) {
432 .equalsIgnoreCase(s2Part)) {
433 return extractInt(s1)
459 // This method only works for attributes whose values are of type int,
460 // double, bool, string and 1-D array of primitive types
461 public static String getAttributeValueAsString(AttributeValue val) {
465 Object value = val.get();
469 TypeInfo type = val.typeInfo();
470 if (type.mType == ValueType.RESOURCEMODEL
471 || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)
472 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
475 if (type.mType == ValueType.ARRAY) {
476 if (type.mBaseType == ValueType.INTEGER) {
477 Integer[] values = (Integer[]) value;
478 if (null == values || values.length < 1) {
481 List<Integer> list = new ArrayList<Integer>();
482 for (Integer i : values) {
485 return list.toString();
486 } else if (type.mBaseType == ValueType.DOUBLE) {
487 Double[] values = (Double[]) value;
488 if (null == values || values.length < 1) {
491 List<Double> list = new ArrayList<Double>();
492 for (Double i : values) {
495 return list.toString();
496 } else if (type.mBaseType == ValueType.BOOLEAN) {
497 Boolean[] values = (Boolean[]) value;
498 if (null == values || values.length < 1) {
501 List<Boolean> list = new ArrayList<Boolean>();
502 for (Boolean i : values) {
505 return list.toString();
506 } else if (type.mBaseType == ValueType.STRING) {
507 String[] values = (String[]) value;
508 if (null == values || values.length < 1) {
511 List<String> list = new ArrayList<String>();
512 for (String i : values) {
515 return list.toString();
520 return String.valueOf(value);
524 public static boolean isUriValid(String resURI) {
525 if (null == resURI || resURI.length() < 2 || resURI.length() > 63
526 || !resURI.startsWith("/") || resURI.endsWith("/")
527 || resURI.contains("/..") || resURI.contains("//")
528 || resURI.contains("/./") || resURI.contains("?")
529 || resURI.contains("#") || resURI.contains("%")) {
535 public static boolean isResourceTypeValid(String resType) {
536 if (null == resType || resType.isEmpty()) {
540 char[] ch = resType.toCharArray();
542 if (c != '.' && c != '-' && (c < 'a' || c > 'z')
543 && (c < '0' || c > '9')) {
551 public static Map<String, String> getResourceInterfaces(
552 Class<? extends Resource> resourceClass) {
553 Map<String, String> ifTypes = null;
554 if (resourceClass == SingleResource.class) {
555 ifTypes = new HashMap<String, String>();
556 ifTypes.put(Constants.BASELINE_INTERFACE, "Baseline");
557 ifTypes.put(Constants.READ_ONLY_INTERFACE, "Read-Only");
558 ifTypes.put(Constants.READ_WRITE_INTERFACE, "Read-Write");
559 ifTypes.put(Constants.ACTUATOR_INTERFACE, "Actuator");
560 ifTypes.put(Constants.SENSOR_INTERFACE, "Sensor");