001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.server.service;
017
018import javax.enterprise.context.ApplicationScoped;
019import javax.enterprise.inject.Disposes;
020import javax.enterprise.inject.Produces;
021import javax.json.bind.Jsonb;
022import javax.json.bind.JsonbBuilder;
023import javax.json.bind.JsonbConfig;
024import javax.json.bind.config.PropertyOrderStrategy;
025
026import org.talend.sdk.component.server.service.qualifier.ComponentServer;
027
028import lombok.extern.slf4j.Slf4j;
029
030@Slf4j
031@ApplicationScoped
032public class JsonbFactory {
033
034    @Produces
035    @ComponentServer
036    @ApplicationScoped
037    Jsonb jsonb() {
038        return JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
039    }
040
041    void destroyJsonb(@Disposes final Jsonb jsonb) {
042        try {
043            jsonb.close();
044        } catch (final Exception e) {
045            log.error(e.getMessage(), e);
046        }
047    }
048}