View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
17   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   */
20  
21  package org.metricshub.wbem.sblim.slp.internal.msg;
22  
23  /*-
24   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
25   * WBEM Java Client
26   * ჻჻჻჻჻჻
27   * Copyright 2023 - 2025 MetricsHub
28   * ჻჻჻჻჻჻
29   * Licensed under the Apache License, Version 2.0 (the "License");
30   * you may not use this file except in compliance with the License.
31   * You may obtain a copy of the License at
32   *
33   *      http://www.apache.org/licenses/LICENSE-2.0
34   *
35   * Unless required by applicable law or agreed to in writing, software
36   * distributed under the License is distributed on an "AS IS" BASIS,
37   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38   * See the License for the specific language governing permissions and
39   * limitations under the License.
40   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
41   */
42  
43  import java.io.IOException;
44  import java.io.InputStream;
45  import java.net.DatagramPacket;
46  import java.net.Socket;
47  import org.metricshub.wbem.sblim.slp.ServiceLocationException;
48  import org.metricshub.wbem.sblim.slp.internal.msg.FunctionIDs;
49  
50  /**
51   * MsgFactory
52   *
53   */
54  public class MsgFactory implements FunctionIDs {
55  
56  	/**
57  	 * FactoryEntry
58  	 *
59  	 */
60  	private interface FactoryEntry {
61  		/**
62  		 * parse
63  		 *
64  		 * @param pHdr
65  		 * @param pInStr
66  		 * @return SLPMessage
67  		 * @throws ServiceLocationException
68  		 * @throws IOException
69  		 */
70  		public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException;
71  	}
72  
73  	private static FactoryEntry[] cFactoryArray;
74  
75  	private static void placeFactory(int pFnID, FactoryEntry pEntry) {
76  		cFactoryArray[pFnID - FIRST_ID] = pEntry;
77  	}
78  
79  	private static synchronized void createFactoryArray() {
80  		if (cFactoryArray != null) return;
81  		cFactoryArray = new FactoryEntry[LAST_ID - FIRST_ID + 1];
82  		for (int i = 0; i < cFactoryArray.length; i++) cFactoryArray[i] = null;
83  		placeFactory(
84  			ATTR_RPLY,
85  			new FactoryEntry() {
86  
87  				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
88  					return AttributeReply.parse(pHdr, pInStr);
89  				}
90  			}
91  		);
92  		placeFactory(
93  			ATTR_RQST,
94  			new FactoryEntry() {
95  
96  				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
97  					return AttributeRequest.parse(pHdr, pInStr);
98  				}
99  			}
100 		);
101 		placeFactory(
102 			DA_ADVERT,
103 			new FactoryEntry() {
104 
105 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
106 					return DAAdvert.parse(pHdr, pInStr);
107 				}
108 			}
109 		);
110 		placeFactory(
111 			SA_ADVERT,
112 			new FactoryEntry() {
113 
114 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
115 					return SAAdvert.parse(pHdr, pInStr);
116 				}
117 			}
118 		);
119 		placeFactory(
120 			SRV_ACK,
121 			new FactoryEntry() {
122 
123 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
124 					return ServiceAcknowledgment.parse(pHdr, pInStr);
125 				}
126 			}
127 		);
128 		placeFactory(
129 			SRV_DEREG,
130 			new FactoryEntry() {
131 
132 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
133 					return ServiceDeregistration.parse(pHdr, pInStr);
134 				}
135 			}
136 		);
137 		placeFactory(
138 			SRV_REG,
139 			new FactoryEntry() {
140 
141 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
142 					return ServiceRegistration.parse(pHdr, pInStr);
143 				}
144 			}
145 		);
146 		placeFactory(
147 			SRV_RPLY,
148 			new FactoryEntry() {
149 
150 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
151 					return ServiceReply.parse(pHdr, pInStr);
152 				}
153 			}
154 		);
155 		placeFactory(
156 			SRV_RQST,
157 			new FactoryEntry() {
158 
159 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
160 					return ServiceRequest.parse(pHdr, pInStr);
161 				}
162 			}
163 		);
164 		//
165 		placeFactory(
166 			SRV_TYPE_RPLY,
167 			new FactoryEntry() {
168 
169 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
170 					return ServiceTypeReply.parse(pHdr, pInStr);
171 				}
172 			}
173 		);
174 		placeFactory(
175 			SRV_TYPE_RQST,
176 			new FactoryEntry() {
177 
178 				public SLPMessage parse(MsgHeader pHdr, SLPInputStream pInStr) throws ServiceLocationException, IOException {
179 					return ServiceTypeRequest.parse(pHdr, pInStr);
180 				}
181 			}
182 		);
183 	}
184 
185 	private static FactoryEntry getFactory(int pFnID) {
186 		createFactoryArray();
187 		return cFactoryArray[pFnID - FIRST_ID];
188 	}
189 
190 	/**
191 	 * parse
192 	 *
193 	 * @param pSock
194 	 * @return SLPMessage
195 	 * @throws ServiceLocationException
196 	 * @throws IOException
197 	 */
198 	public static SLPMessage parse(Socket pSock) throws ServiceLocationException, IOException {
199 		return parse(pSock.getInputStream());
200 	}
201 
202 	/**
203 	 * parse
204 	 *
205 	 * @param pInStr
206 	 * @return SLPMessage
207 	 * @throws ServiceLocationException
208 	 * @throws IOException
209 	 */
210 	public static SLPMessage parse(InputStream pInStr) throws ServiceLocationException, IOException {
211 		return parse(new SLPInputStream(pInStr));
212 	}
213 
214 	/**
215 	 * parse
216 	 *
217 	 * @param pPacket
218 	 * @return SLPMessage
219 	 * @throws ServiceLocationException
220 	 * @throws IOException
221 	 */
222 	public static SLPMessage parse(DatagramPacket pPacket) throws ServiceLocationException, IOException {
223 		return parse(new SLPInputStream(pPacket));
224 	}
225 
226 	/**
227 	 * parse
228 	 *
229 	 * @param pInStr
230 	 * @return SLPMessage
231 	 * @throws ServiceLocationException
232 	 * @throws IOException
233 	 */
234 	public static SLPMessage parse(SLPInputStream pInStr) throws ServiceLocationException, IOException {
235 		MsgHeader hdr = MsgHeader.parse(pInStr);
236 		FactoryEntry factory = getFactory(hdr.getFunctionID());
237 		if (factory == null) throw new ServiceLocationException(
238 			ServiceLocationException.NOT_IMPLEMENTED,
239 			"FunctionID=" + hdr.getFunctionID() + " is not implemented!"
240 		);
241 		return factory.parse(hdr, pInStr);
242 	}
243 }