OSCoreCoapStackFactory.java

  1. /*******************************************************************************
  2. + * Copyright (c) 2018 Bosch Software Innovations GmbH and others.
  3. + *
  4. + * All rights reserved. This program and the accompanying materials
  5. + * are made available under the terms of the Eclipse Public License v2.0
  6. + * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. + *
  8. + * The Eclipse Public License is available at
  9. + *    http://www.eclipse.org/legal/epl-v20.html
  10. + * and the Eclipse Distribution License is available at
  11. + *    http://www.eclipse.org/org/documents/edl-v10.html.
  12. + *
  13. + * Contributors:
  14. + *    Bosch Software Innovations GmbH - initial implementation.
  15. + ******************************************************************************/
  16. package org.eclipse.californium.oscore;

  17. import java.util.concurrent.atomic.AtomicBoolean;

  18. import org.eclipse.californium.core.coap.CoAP;
  19. import org.eclipse.californium.core.network.CoapEndpoint;
  20. import org.eclipse.californium.core.network.CoapStackFactory;
  21. import org.eclipse.californium.core.network.Outbox;
  22. import org.eclipse.californium.core.network.stack.CoapStack;
  23. import org.eclipse.californium.elements.config.Configuration;

  24. /**
  25.  * Coap stack factory creating a {@link OSCoreStack} including a
  26.  * {@link ObjectSecurityLayer}.
  27.  */
  28. public class OSCoreCoapStackFactory implements CoapStackFactory {

  29.     private static AtomicBoolean init = new AtomicBoolean();
  30.     private static volatile OSCoreCtxDB defaultCtxDb;

  31.     @Override
  32.     public CoapStack createCoapStack(String protocol, String tag, Configuration config, Outbox outbox, Object customStackArgument) {
  33.         if (CoAP.isTcpProtocol(protocol)) {
  34.             throw new IllegalArgumentException("protocol \"" + protocol + "\" is not supported!");
  35.         }
  36.         OSCoreCtxDB ctxDb = defaultCtxDb;
  37.         if (customStackArgument != null) {
  38.             ctxDb = (OSCoreCtxDB) customStackArgument;
  39.         }
  40.         return new OSCoreStack(tag, config, outbox, ctxDb);
  41.     }

  42.     /**
  43.      * Use {@link OSCoreStack} as default for {@link CoapEndpoint}.
  44.      *
  45.      * Note: the factory is only applied once with the first call, the
  46.      * {@link #defaultCtxDb} is update on every call.
  47.      *
  48.      * @param defaultCtxDb default context DB. Passed in as default argument for {@link OSCoreStack}
  49.      *
  50.      * @see CoapEndpoint#setDefaultCoapStackFactory(CoapStackFactory)
  51.      */
  52.     public static void useAsDefault(OSCoreCtxDB defaultCtxDb) {
  53.         if (init.compareAndSet(false, true)) {
  54.             CoapEndpoint.setDefaultCoapStackFactory(new OSCoreCoapStackFactory());
  55.         }
  56.         OSCoreCoapStackFactory.defaultCtxDb = defaultCtxDb;
  57.     }
  58. }