18 * |
18 * |
19 * CDDL HEADER END |
19 * CDDL HEADER END |
20 */ |
20 */ |
21 |
21 |
22 /* |
22 /* |
23 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
23 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
24 */ |
24 */ |
25 |
25 |
26 package com.oracle.solaris.vp.panel.common.smf; |
26 package com.oracle.solaris.vp.panel.common.smf; |
27 |
27 |
28 import java.text.DateFormat; |
28 import java.text.DateFormat; |
29 import java.util.*; |
29 import java.util.*; |
30 import javax.management.*; |
30 import com.oracle.solaris.rad.client.ADRName; |
31 import com.oracle.solaris.scf.common.FMRI; |
31 import com.oracle.solaris.scf.common.FMRI; |
32 import com.oracle.solaris.vp.panel.common.api.panel.MBeanUtil; |
32 import com.oracle.solaris.vp.panel.common.api.smf_old.ServiceInfo; |
33 import com.oracle.solaris.vp.panel.common.api.smf_old.SmfState; |
33 import com.oracle.solaris.vp.panel.common.api.smf_old.SmfState; |
34 import com.oracle.solaris.vp.panel.common.model.ManagedObjectStatus; |
34 import com.oracle.solaris.vp.panel.common.model.ManagedObjectStatus; |
35 import com.oracle.solaris.vp.util.misc.finder.Finder; |
35 import com.oracle.solaris.vp.util.misc.finder.Finder; |
36 |
36 |
37 public class ServiceUtil { |
37 public class ServiceUtil { |
38 // |
38 // |
39 // Static data |
39 // Static data |
40 // |
40 // |
41 |
41 |
42 public static final String SERVICE_DOMAIN = |
42 public static final String SERVICE_DOMAIN = |
43 MBeanUtil.VP_DOMAIN + ".smf_old"; |
43 "com.oracle.solaris.vp.panel.common.api.smf_old"; |
44 |
44 |
45 // |
45 // |
46 // Static methods |
46 // Static methods |
47 // |
47 // |
48 |
48 |
49 public static ObjectName getServiceObjectName( |
49 public static ADRName getServiceObjectName( |
50 String service, String instance) throws MalformedObjectNameException { |
50 String service, String instance) { |
51 |
51 |
52 /* |
52 String namestr = String.format("%s:type=ServiceInfo,service=%s", |
53 * We don't use ObjectName.quote() because we can be called to |
|
54 * construct a pattern, and no valid FMRI characters require quoting |
|
55 * in a quoted ObjectName values. |
|
56 */ |
|
57 String namestr = String.format("%s:type=service,service=\"%s\"", |
|
58 SERVICE_DOMAIN, service); |
53 SERVICE_DOMAIN, service); |
59 if (instance != null) |
54 if (instance != null) |
60 namestr = String.format("%s,instance=\"%s\"", namestr, instance); |
55 namestr = String.format("%s,instance=%s", namestr, instance); |
61 |
56 |
62 return (new ObjectName(namestr)); |
57 return (new ADRName(namestr)); |
63 } |
58 } |
64 |
59 |
65 public static ObjectName toObjectName(String service, String instance) |
60 public static ADRName toADRName(String service, String instance) { |
66 { |
|
67 try { |
61 try { |
68 return (getServiceObjectName(service, instance)); |
62 return (getServiceObjectName(service, instance)); |
69 } catch (MalformedObjectNameException e) { |
63 } catch (Exception e) { |
70 throw (new IllegalArgumentException(e)); |
64 throw (new IllegalArgumentException(e)); |
71 } |
65 } |
72 } |
66 } |
73 |
67 |
74 public static ObjectName toObjectName(FMRI fmri) |
68 public static ADRName toADRName(FMRI fmri) { |
75 { |
|
76 switch (fmri.getSvcType()) { |
69 switch (fmri.getSvcType()) { |
77 case INSTANCE: |
70 case INSTANCE: |
78 return (toObjectName(fmri.getService(), fmri.getInstance())); |
71 return (toADRName(fmri.getService(), fmri.getInstance())); |
79 case SERVICE: |
72 case SERVICE: |
80 return (toObjectName(fmri.getService(), null)); |
73 return (toADRName(fmri.getService(), null)); |
81 default: |
74 default: |
82 return (null); |
75 return (null); |
83 } |
76 } |
84 } |
77 } |
85 |
78 |
86 public static String toFMRI(ObjectName on) |
79 public static String toFMRI(ADRName an) { |
87 { |
80 |
88 String service = ObjectName.unquote(on.getKeyProperty("service")); |
81 String name = an.toString(); |
89 String instance = ObjectName.unquote(on.getKeyProperty("instance")); |
82 String[] pieces = name.split(":", 2); |
90 |
83 String[] kvpairs = pieces[1].split(","); |
91 if (!on.getDomain().equals(SERVICE_DOMAIN) || service == null) |
84 String domain = pieces[0]; |
92 throw (new IllegalArgumentException("Not an SMF ObjectName")); |
85 |
93 |
86 Map<String, String> kvs = new HashMap<String, String>(); |
94 service = service.replace('+', ','); |
87 for (String s : kvpairs) { |
95 if (instance != null) |
88 String[] kv = s.split("="); |
96 instance = instance.replace('+', ','); |
89 kvs.put(kv[0], kv[1]); |
|
90 } |
|
91 |
|
92 String service = kvs.get("service"); |
|
93 String instance = kvs.get("instance"); |
|
94 if (!domain.equals(SERVICE_DOMAIN) || service == null) |
|
95 throw new IllegalArgumentException("Not an SMF Object name"); |
97 |
96 |
98 return ("svc:/" + (instance == null ? |
97 return ("svc:/" + (instance == null ? |
99 service : service + ":" + instance)); |
98 service : service + ":" + instance)); |
100 } |
99 } |
101 |
100 |
102 public static String toService(ObjectName on) |
101 public static String toService(ADRName an) { |
103 { |
102 |
104 try { |
103 String name = an.toString(); |
105 String service = on.getKeyProperty("service"); |
104 String[] pieces = name.split(":", 2); |
106 if (on.getDomain().equals(SERVICE_DOMAIN) && service != null) |
105 String[] kvpairs = pieces[1].split(","); |
107 return ObjectName.unquote(service); |
106 String domain = pieces[0]; |
108 } catch (IllegalArgumentException e) { |
107 String service = null; |
109 } |
108 |
110 throw new IllegalArgumentException("Not an SMF ObjectName"); |
109 for (int i = 0; i < kvpairs.length; i++) { |
|
110 if (kvpairs[i].startsWith("service=")) { |
|
111 String[] kv = kvpairs[i].split("="); |
|
112 service = kv[1]; |
|
113 break; |
|
114 } |
|
115 } |
|
116 |
|
117 if (domain.equals(SERVICE_DOMAIN) && service != null) |
|
118 return service; |
|
119 else |
|
120 throw new IllegalArgumentException("Not an SMF Object name"); |
111 } |
121 } |
112 |
122 |
113 public static ManagedObjectStatus getPanelStatus(SmfState state) { |
123 public static ManagedObjectStatus getPanelStatus(SmfState state) { |
114 ManagedObjectStatus status = ManagedObjectStatus.ERROR; |
124 ManagedObjectStatus status = ManagedObjectStatus.ERROR; |
115 |
125 |