| 1 | /* |
| 2 | * Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json.spi; |
| 18 | |
| 19 | import java.io.InputStream; |
| 20 | import java.io.OutputStream; |
| 21 | import java.io.Reader; |
| 22 | import java.io.Writer; |
| 23 | import java.lang.reflect.Method; |
| 24 | import java.math.BigDecimal; |
| 25 | import java.math.BigInteger; |
| 26 | import java.security.AccessController; |
| 27 | import java.security.PrivilegedAction; |
| 28 | import java.util.Collection; |
| 29 | import java.util.Iterator; |
| 30 | import java.util.Map; |
| 31 | import java.util.Optional; |
| 32 | import java.util.ServiceLoader; |
| 33 | import java.util.logging.Level; |
| 34 | import java.util.logging.Logger; |
| 35 | |
| 36 | import jakarta.json.JsonArray; |
| 37 | import jakarta.json.JsonArrayBuilder; |
| 38 | import jakarta.json.JsonBuilderFactory; |
| 39 | import jakarta.json.JsonException; |
| 40 | import jakarta.json.JsonMergePatch; |
| 41 | import jakarta.json.JsonNumber; |
| 42 | import jakarta.json.JsonObject; |
| 43 | import jakarta.json.JsonObjectBuilder; |
| 44 | import jakarta.json.JsonPatch; |
| 45 | import jakarta.json.JsonPatchBuilder; |
| 46 | import jakarta.json.JsonPointer; |
| 47 | import jakarta.json.JsonReader; |
| 48 | import jakarta.json.JsonReaderFactory; |
| 49 | import jakarta.json.JsonString; |
| 50 | import jakarta.json.JsonStructure; |
| 51 | import jakarta.json.JsonValue; |
| 52 | import jakarta.json.JsonWriter; |
| 53 | import jakarta.json.JsonWriterFactory; |
| 54 | import jakarta.json.stream.JsonGenerator; |
| 55 | import jakarta.json.stream.JsonGeneratorFactory; |
| 56 | import jakarta.json.stream.JsonParser; |
| 57 | import jakarta.json.stream.JsonParserFactory; |
| 58 | |
| 59 | /** |
| 60 | * Service provider for JSON processing objects. |
| 61 | * |
| 62 | * <p>All the methods in this class are safe for use by multiple concurrent |
| 63 | * threads. |
| 64 | * |
| 65 | * @see ServiceLoader |
| 66 | */ |
| 67 | public abstract class JsonProvider { |
| 68 | |
| 69 | /** |
| 70 | * The name of the property that contains the name of the class capable of creating new JsonProvider objects. |
| 71 | */ |
| 72 | public static final String JSONP_PROVIDER_FACTORY = "jakarta.json.provider"; |
| 73 | |
| 74 | /** |
| 75 | * A constant representing the name of the default |
| 76 | * {@code JsonProvider} implementation class. |
| 77 | */ |
| 78 | private static final String DEFAULT_PROVIDER |
| 79 | = "org.eclipse.parsson.JsonProviderImpl"; |
| 80 | |
| 81 | /** A logger */ |
| 82 | private static final Logger LOG = Logger.getLogger(JsonProvider.class.getName()); |
| 83 | |
| 84 | /** |
| 85 | * Default constructor. |
| 86 | */ |
| 87 | protected JsonProvider() { |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a JSON provider object. |
| 92 | * |
| 93 | * Implementation discovery consists of following steps: |
| 94 | * <ol> |
| 95 | * <li>If the system property {@value #JSONP_PROVIDER_FACTORY} exists, |
| 96 | * then its value is assumed to be the provider factory class. |
| 97 | * This phase of the look up enables per-JVM override of the JsonProvider implementation.</li> |
| 98 | * <li>The provider is loaded using the {@link ServiceLoader#load(Class)} method.</li> |
| 99 | * <li>If all the steps above fail, then the rest of the look up is unspecified. That said, |
| 100 | * the recommended behavior is to simply look for some hard-coded platform default Jakarta |
| 101 | * JSON Processing implementation. This phase of the look up is so that a platform can have |
| 102 | * its own Jakarta JSON Processing implementation as the last resort.</li> |
| 103 | * </ol> |
| 104 | * Users are recommended to cache the result of this method. |
| 105 | * |
| 106 | * @see ServiceLoader |
| 107 | * @return a JSON provider |
| 108 | */ |
| 109 | public static JsonProvider provider() { |
| 110 | LOG.log(Level.FINE, "Checking system property {0}", JSONP_PROVIDER_FACTORY); |
| 111 | final String factoryClassName = System.getSecurityManager() != null |
| 112 | ? AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(JSONP_PROVIDER_FACTORY)) |
| 113 | : System.getProperty(JSONP_PROVIDER_FACTORY); |
| 114 | if (factoryClassName != null) { |
| 115 | JsonProvider provider = newInstance(factoryClassName); |
| 116 | LOG.log(Level.FINE, "System property used; returning object [{0}]", |
| 117 | provider.getClass().getName()); |
| 118 | return provider; |
| 119 | } |
| 120 | |
| 121 | LOG.log(Level.FINE, "Checking ServiceLoader"); |
| 122 | ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class); |
| 123 | Iterator<JsonProvider> it = loader.iterator(); |
| 124 | if (it.hasNext()) { |
| 125 | JsonProvider provider = it.next(); |
| 126 | LOG.log(Level.FINE, "ServiceLoader loading Facility used; returning object [{0}]", |
| 127 | provider.getClass().getName()); |
| 128 | return provider; |
| 129 | } |
| 130 | |
| 131 | // handling OSGi (specific default) |
| 132 | if (isOsgi()) { |
| 133 | LOG.log(Level.FINE, "Checking OSGi"); |
| 134 | JsonProvider provider = lookupUsingOSGiServiceLoader(JsonProvider.class); |
| 135 | if (provider != null) { |
| 136 | LOG.log(Level.FINE, "OSGi loading facility used; returning object [{0}].", |
| 137 | provider.getClass().getName()); |
| 138 | return provider; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // else no provider found |
| 143 | LOG.fine("Trying to create the platform default provider"); |
| 144 | return newInstance(DEFAULT_PROVIDER); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Creates a new instance from the specified class |
| 149 | * @param className name of the class to instantiate |
| 150 | * @return the JsonProvider instance |
| 151 | * @throws JsonException for issues during creation of an instance of the JsonProvider |
| 152 | */ |
| 153 | private static JsonProvider newInstance(String className) { |
| 154 | try { |
| 155 | checkPackageAccess(className); |
| 156 | @SuppressWarnings({"unchecked"}) |
| 157 | Class<JsonProvider> clazz = (Class<JsonProvider>) Class.forName(className); |
| 158 | return clazz.getConstructor().newInstance(); |
| 159 | } catch (ClassNotFoundException x) { |
| 160 | throw new JsonException( |
| 161 | "Provider " + className + " not found", x); |
| 162 | } catch (Exception x) { |
| 163 | throw new JsonException( |
| 164 | "Provider " + className + " could not be instantiated: " + x, |
| 165 | x); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Make sure that the current thread has an access to the package of the given name. |
| 171 | * @param className The class name to check. |
| 172 | */ |
| 173 | private static void checkPackageAccess(String className) { |
| 174 | SecurityManager s = System.getSecurityManager(); |
| 175 | if (s != null) { |
| 176 | int i = className.lastIndexOf('.'); |
| 177 | if (i != -1) { |
| 178 | s.checkPackageAccess(className.substring(0, i)); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Creates a JSON parser from a character stream. |
| 185 | * |
| 186 | * @param reader i/o reader from which JSON is to be read |
| 187 | * @return a JSON parser |
| 188 | */ |
| 189 | public abstract JsonParser createParser(Reader reader); |
| 190 | |
| 191 | /** |
| 192 | * Creates a JSON parser from the specified byte stream. |
| 193 | * The character encoding of the stream is determined |
| 194 | * as defined in <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159 |
| 195 | * </a>. |
| 196 | * |
| 197 | * @param in i/o stream from which JSON is to be read |
| 198 | * @throws JsonException if encoding cannot be determined |
| 199 | * or i/o error (IOException would be cause of JsonException) |
| 200 | * @return a JSON parser |
| 201 | */ |
| 202 | public abstract JsonParser createParser(InputStream in); |
| 203 | |
| 204 | /** |
| 205 | * Creates a parser factory for creating {@link JsonParser} instances. |
| 206 | * |
| 207 | * @return a JSON parser factory |
| 208 | * |
| 209 | public abstract JsonParserFactory createParserFactory(); |
| 210 | */ |
| 211 | |
| 212 | /** |
| 213 | * Creates a parser factory for creating {@link JsonParser} instances. |
| 214 | * The factory is configured with the specified map of |
| 215 | * provider specific configuration properties. Provider implementations |
| 216 | * should ignore any unsupported configuration properties specified in |
| 217 | * the map. |
| 218 | * |
| 219 | * @param config a map of provider specific properties to configure the |
| 220 | * JSON parsers. The map may be empty or null |
| 221 | * @return a JSON parser factory |
| 222 | */ |
| 223 | public abstract JsonParserFactory createParserFactory(Map<String, ?> config); |
| 224 | |
| 225 | /** |
| 226 | * Creates a JSON generator for writing JSON text to a character stream. |
| 227 | * |
| 228 | * @param writer a i/o writer to which JSON is written |
| 229 | * @return a JSON generator |
| 230 | */ |
| 231 | public abstract JsonGenerator createGenerator(Writer writer); |
| 232 | |
| 233 | /** |
| 234 | * Creates a JSON generator for writing JSON text to a byte stream. |
| 235 | * |
| 236 | * @param out i/o stream to which JSON is written |
| 237 | * @return a JSON generator |
| 238 | */ |
| 239 | public abstract JsonGenerator createGenerator(OutputStream out); |
| 240 | |
| 241 | /** |
| 242 | * Creates a generator factory for creating {@link JsonGenerator} instances. |
| 243 | * |
| 244 | * @return a JSON generator factory |
| 245 | * |
| 246 | public abstract JsonGeneratorFactory createGeneratorFactory(); |
| 247 | */ |
| 248 | |
| 249 | /** |
| 250 | * Creates a generator factory for creating {@link JsonGenerator} instances. |
| 251 | * The factory is configured with the specified map of provider specific |
| 252 | * configuration properties. Provider implementations should |
| 253 | * ignore any unsupported configuration properties specified in the map. |
| 254 | * |
| 255 | * @param config a map of provider specific properties to configure the |
| 256 | * JSON generators. The map may be empty or null |
| 257 | * @return a JSON generator factory |
| 258 | */ |
| 259 | public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config); |
| 260 | |
| 261 | /** |
| 262 | * Creates a JSON reader from a character stream. |
| 263 | * |
| 264 | * @param reader a reader from which JSON is to be read |
| 265 | * @return a JSON reader |
| 266 | */ |
| 267 | public abstract JsonReader createReader(Reader reader); |
| 268 | |
| 269 | /** |
| 270 | * Creates a JSON reader from a byte stream. The character encoding of |
| 271 | * the stream is determined as described in |
| 272 | * <a href="http://tools.ietf.org/rfc/rfc7159.txt">RFC 7159</a>. |
| 273 | * |
| 274 | * @param in a byte stream from which JSON is to be read |
| 275 | * @return a JSON reader |
| 276 | */ |
| 277 | public abstract JsonReader createReader(InputStream in); |
| 278 | |
| 279 | /** |
| 280 | * Creates a JSON writer to write a |
| 281 | * JSON {@link JsonObject object} or {@link JsonArray array} |
| 282 | * structure to the specified character stream. |
| 283 | * |
| 284 | * @param writer to which JSON object or array is written |
| 285 | * @return a JSON writer |
| 286 | */ |
| 287 | public abstract JsonWriter createWriter(Writer writer); |
| 288 | |
| 289 | /** |
| 290 | * Creates a JSON writer to write a |
| 291 | * JSON {@link JsonObject object} or {@link JsonArray array} |
| 292 | * structure to the specified byte stream. Characters written to |
| 293 | * the stream are encoded into bytes using UTF-8 encoding. |
| 294 | * |
| 295 | * @param out to which JSON object or array is written |
| 296 | * @return a JSON writer |
| 297 | */ |
| 298 | public abstract JsonWriter createWriter(OutputStream out); |
| 299 | |
| 300 | /** |
| 301 | * Creates a writer factory for creating {@link JsonWriter} objects. |
| 302 | * The factory is configured with the specified map of provider specific |
| 303 | * configuration properties. Provider implementations should ignore any |
| 304 | * unsupported configuration properties specified in the map. |
| 305 | * |
| 306 | * @param config a map of provider specific properties to configure the |
| 307 | * JSON writers. The map may be empty or null |
| 308 | * @return a JSON writer factory |
| 309 | */ |
| 310 | public abstract JsonWriterFactory createWriterFactory(Map<String,?> config); |
| 311 | |
| 312 | /** |
| 313 | * Creates a reader factory for creating {@link JsonReader} objects. |
| 314 | * The factory is configured with the specified map of provider specific |
| 315 | * configuration properties. Provider implementations should ignore any |
| 316 | * unsupported configuration properties specified in the map. |
| 317 | * |
| 318 | * @param config a map of provider specific properties to configure the |
| 319 | * JSON readers. The map may be empty or null |
| 320 | * @return a JSON reader factory |
| 321 | */ |
| 322 | public abstract JsonReaderFactory createReaderFactory(Map<String,?> config); |
| 323 | |
| 324 | /** |
| 325 | * Creates a JSON object builder. |
| 326 | * |
| 327 | * @return a JSON object builder |
| 328 | */ |
| 329 | public abstract JsonObjectBuilder createObjectBuilder(); |
| 330 | |
| 331 | /** |
| 332 | * Creates a JSON object builder, initialized with the specified object. |
| 333 | * |
| 334 | * @param object the initial JSON object in the builder |
| 335 | * @return a JSON object builder |
| 336 | * |
| 337 | * @since 1.1 |
| 338 | */ |
| 339 | public JsonObjectBuilder createObjectBuilder(JsonObject object) { |
| 340 | throw new UnsupportedOperationException(); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Creates a JSON object builder, initialized with the data from specified {@code map}. |
| 345 | * If the @{code map} contains {@link Optional}s then resulting JSON object builder |
| 346 | * contains the key from the {@code map} only if the {@link Optional} is not empty. |
| 347 | * |
| 348 | * @param map the initial object in the builder |
| 349 | * @return a JSON object builder |
| 350 | * @exception IllegalArgumentException if the value from the {@code map} cannot be converted |
| 351 | * to the corresponding {@link JsonValue} |
| 352 | * |
| 353 | * @since 1.1 |
| 354 | */ |
| 355 | public JsonObjectBuilder createObjectBuilder(Map<String, ?> map) { |
| 356 | throw new UnsupportedOperationException(); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Creates a JSON array builder. |
| 361 | * |
| 362 | * @return a JSON array builder |
| 363 | */ |
| 364 | public abstract JsonArrayBuilder createArrayBuilder(); |
| 365 | |
| 366 | /** |
| 367 | * Creates a JSON array builder, initialized with the specified array. |
| 368 | * |
| 369 | * @param array the initial JSON array in the builder |
| 370 | * @return a JSON array builder |
| 371 | * |
| 372 | * @since 1.1 |
| 373 | */ |
| 374 | public JsonArrayBuilder createArrayBuilder(JsonArray array) { |
| 375 | throw new UnsupportedOperationException(); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Creates JSON Pointer (<a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>) |
| 380 | * from given {@code jsonPointer} string. |
| 381 | * <ul> |
| 382 | * <li>An empty {@code jsonPointer} string defines a reference to the target itself.</li> |
| 383 | * <li>If the {@code jsonPointer} string is non-empty, it must be a sequence of '{@code /}' prefixed tokens.</li> |
| 384 | * </ul> |
| 385 | * |
| 386 | * @param jsonPointer the JSON Pointer string |
| 387 | * @throws NullPointerException if {@code jsonPointer} is {@code null} |
| 388 | * @throws JsonException if {@code jsonPointer} is not a valid JSON Pointer |
| 389 | * @return a JSON Pointer |
| 390 | * |
| 391 | * @since 1.1 |
| 392 | */ |
| 393 | public JsonPointer createPointer(String jsonPointer) { |
| 394 | throw new UnsupportedOperationException(); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Creates a JSON Patch builder (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>). |
| 399 | * |
| 400 | * @return a JSON Patch builder |
| 401 | * |
| 402 | * @since 1.1 |
| 403 | */ |
| 404 | public JsonPatchBuilder createPatchBuilder() { |
| 405 | throw new UnsupportedOperationException(); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Creates a JSON Patch builder |
| 410 | * (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>), |
| 411 | * initialized with the specified operations. |
| 412 | * |
| 413 | * @param array the initial patch operations |
| 414 | * @return a JSON Patch builder |
| 415 | * |
| 416 | * @since 1.1 |
| 417 | */ |
| 418 | public JsonPatchBuilder createPatchBuilder(JsonArray array) { |
| 419 | throw new UnsupportedOperationException(); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Creates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>) |
| 424 | * from the specified operations. |
| 425 | * |
| 426 | * @param array patch operations |
| 427 | * @return a JSON Patch |
| 428 | * |
| 429 | * @since 1.1 |
| 430 | */ |
| 431 | public JsonPatch createPatch(JsonArray array) { |
| 432 | throw new UnsupportedOperationException(); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Generates a JSON Patch (<a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>) |
| 437 | * from the source and target {@code JsonStructure}. |
| 438 | * The generated JSON Patch need not be unique. |
| 439 | * |
| 440 | * @param source the source |
| 441 | * @param target the target, must be the same type as the source |
| 442 | * @return a JSON Patch which when applied to the source, yields the target |
| 443 | * |
| 444 | * @since 1.1 |
| 445 | */ |
| 446 | public JsonPatch createDiff(JsonStructure source, JsonStructure target) { |
| 447 | throw new UnsupportedOperationException(); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Creates JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>) |
| 452 | * from specified {@code JsonValue}. |
| 453 | * |
| 454 | * @param patch the patch |
| 455 | * @return a JSON Merge Patch |
| 456 | * |
| 457 | * @since 1.1 |
| 458 | */ |
| 459 | public JsonMergePatch createMergePatch(JsonValue patch) { |
| 460 | throw new UnsupportedOperationException(); |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Generates a JSON Merge Patch (<a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>) |
| 465 | * from the source and target {@code JsonValue}s |
| 466 | * which when applied to the {@code source}, yields the {@code target}. |
| 467 | * |
| 468 | * @param source the source |
| 469 | * @param target the target |
| 470 | * @return a JSON Merge Patch |
| 471 | * |
| 472 | * @since 1.1 |
| 473 | */ |
| 474 | public JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) { |
| 475 | throw new UnsupportedOperationException(); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Creates a JSON array builder, initialized with the content of specified {@code collection}. |
| 480 | * If the @{code collection} contains {@link Optional}s then resulting JSON array builder |
| 481 | * contains the value from the {@code collection} only if the {@link Optional} is not empty. |
| 482 | * |
| 483 | * @param collection the initial data for the builder |
| 484 | * @return a JSON array builder |
| 485 | * @exception IllegalArgumentException if the value from the {@code collection} cannot be converted |
| 486 | * to the corresponding {@link JsonValue} |
| 487 | * |
| 488 | * @since 1.1 |
| 489 | */ |
| 490 | public JsonArrayBuilder createArrayBuilder(Collection<?> collection) { |
| 491 | throw new UnsupportedOperationException(); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | /** |
| 496 | * Creates a builder factory for creating {@link JsonArrayBuilder} |
| 497 | * and {@link JsonObjectBuilder} objects. |
| 498 | * The factory is configured with the specified map of provider specific |
| 499 | * configuration properties. Provider implementations should ignore any |
| 500 | * unsupported configuration properties specified in the map. |
| 501 | * |
| 502 | * @param config a map of provider specific properties to configure the |
| 503 | * JSON builders. The map may be empty or null |
| 504 | * @return a JSON builder factory |
| 505 | */ |
| 506 | public abstract JsonBuilderFactory createBuilderFactory(Map<String,?> config); |
| 507 | |
| 508 | /** |
| 509 | * Creates a JsonString. |
| 510 | * |
| 511 | * @param value a JSON string |
| 512 | * @return the JsonString for the string |
| 513 | * |
| 514 | * @since 1.1 |
| 515 | */ |
| 516 | public JsonString createValue(String value) { |
| 517 | throw new UnsupportedOperationException(); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Creates a JsonNumber. |
| 522 | * |
| 523 | * @param value a JSON number |
| 524 | * @return the JsonNumber for the number |
| 525 | * |
| 526 | * @since 1.1 |
| 527 | */ |
| 528 | public JsonNumber createValue(int value) { |
| 529 | throw new UnsupportedOperationException(); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Creates a JsonNumber. |
| 534 | * |
| 535 | * @param value a JSON number |
| 536 | * @return the JsonNumber for the number |
| 537 | * |
| 538 | * @since 1.1 |
| 539 | */ |
| 540 | public JsonNumber createValue(long value) { |
| 541 | throw new UnsupportedOperationException(); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Creates a JsonNumber. |
| 546 | * |
| 547 | * @param value a JSON number |
| 548 | * @return the JsonNumber for the number |
| 549 | * |
| 550 | * @since 1.1 |
| 551 | */ |
| 552 | public JsonNumber createValue(double value) { |
| 553 | throw new UnsupportedOperationException(); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Creates a JsonNumber. |
| 558 | * |
| 559 | * @param value a JSON number |
| 560 | * @return the JsonNumber for the number |
| 561 | * |
| 562 | * @since 1.1 |
| 563 | */ |
| 564 | public JsonNumber createValue(BigDecimal value) { |
| 565 | throw new UnsupportedOperationException(); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Creates a JsonNumber. |
| 570 | * |
| 571 | * @param value a JSON number |
| 572 | * @return the JsonNumber for the number |
| 573 | * |
| 574 | * @since 1.1 |
| 575 | */ |
| 576 | public JsonNumber createValue(BigInteger value) { |
| 577 | throw new UnsupportedOperationException(); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Creates a JsonNumber. |
| 582 | * |
| 583 | * When it is not implemented it checks the type and delegates |
| 584 | * to an existing method that already handles that type. It throws |
| 585 | * UnsupportedOperationException in case the type is not known. |
| 586 | * |
| 587 | * @param number a JSON number |
| 588 | * @return the JsonNumber for the number |
| 589 | * |
| 590 | * @since 2.1 |
| 591 | */ |
| 592 | public JsonNumber createValue(Number number) { |
| 593 | if (number instanceof Integer) { |
| 594 | return createValue(number.intValue()); |
| 595 | } else if (number instanceof Long) { |
| 596 | return createValue(number.longValue()); |
| 597 | } else if (number instanceof Double) { |
| 598 | return createValue(number.doubleValue()); |
| 599 | } else if (number instanceof BigInteger) { |
| 600 | return createValue((BigInteger) number); |
| 601 | } else if (number instanceof BigDecimal) { |
| 602 | return createValue((BigDecimal) number); |
| 603 | } else { |
| 604 | throw new UnsupportedOperationException(number + " type is not known"); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /** OSGI aware service loader by HK2 */ |
| 609 | private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader"; |
| 610 | |
| 611 | /** |
| 612 | * Check availability of HK2 service loader. |
| 613 | * |
| 614 | * @return true if HK2 service locator is available |
| 615 | */ |
| 616 | private static boolean isOsgi() { |
| 617 | try { |
| 618 | Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); |
| 619 | return true; |
| 620 | } catch (ClassNotFoundException ignored) { |
| 621 | } |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Lookup the service class by the HK2 service locator. |
| 627 | * |
| 628 | * @param serviceClass service class |
| 629 | * @param <T> type of the service |
| 630 | * @return a provider |
| 631 | */ |
| 632 | private static <T> T lookupUsingOSGiServiceLoader(Class<? extends T> serviceClass) { |
| 633 | try { |
| 634 | // Use reflection to avoid having any dependendcy on HK2 ServiceLoader class |
| 635 | Class<?>[] args = new Class<?>[]{serviceClass}; |
| 636 | Class<?> target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); |
| 637 | Method m = target.getMethod("lookupProviderInstances", Class.class); |
| 638 | @SuppressWarnings({"unchecked"}) |
| 639 | Iterator<? extends T> iter = ((Iterable<? extends T>) m.invoke(null, (Object[]) args)).iterator(); |
| 640 | return iter.hasNext() ? iter.next() : null; |
| 641 | } catch (Exception ex) { |
| 642 | LOG.log(Level.FINE, "Unable to find from OSGi: [" + serviceClass.getName() + "]", ex); |
| 643 | return null; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | } |