com.tc.apn.push.core.config.AppConfigFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.tc.apn.push.core.config.AppConfigFactory.java

Source

/*
 *  Copyright Tek Counsel LLC 2013
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.tc.apn.push.core.config;

import java.io.InputStream;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.NotesException;
import lotus.domino.Session;

import org.apache.commons.io.IOUtils;

import com.google.inject.Inject;
import com.tc.di.guicer.IGuicer;
import com.tc.guice.domino.module.SessionFactory;
import com.tc.utils.CloseUtils;

public class AppConfigFactory implements IAppConfigFactory {

    @Inject
    IGuicer guicer;

    private static Map<String, AppConfig> cache = new ConcurrentHashMap<String, AppConfig>();

    private static final Logger logger = Logger.getLogger(AppConfigFactory.class.getName());

    @Override
    public AppConfig provideAppConfig(String appId) {
        InputStream in = null;
        AppConfig cfg = cache.get(appId);

        //if its cached break out....
        if (cfg != null) {
            return cfg;
        }

        cfg = new AppConfig();
        Session session = SessionFactory.openSession();
        try {

            Database db = session.getDatabase(null, PushConstants.PUSH_NSF);

            if (db == null) {
                throw new RuntimeException(PushConstants.PUSH_NSF
                        + " may not be setup, or the server's Id does not have access to it.");
            }

            Document docCfg = db.getView(PushConstants.VIEW_APP_CONFIG).getDocumentByKey(appId, true);

            if (docCfg == null) {
                throw new RuntimeException("No config document found for appId " + appId);
            }

            //build the config object.
            cfg = this.buildConfig(docCfg);

            //cleanup
            docCfg.recycle();
            db.recycle();

        } catch (NotesException n) {
            logger.log(Level.SEVERE, null, n);

        } finally {
            CloseUtils.close(in);
            SessionFactory.closeSession(session);
        }

        cache.put(appId, cfg);

        return cfg;
    }

    private AppConfig buildConfig(Document docCfg) {
        InputStream in = null;

        AppConfig cfg = new AppConfig();

        try {
            Session session = docCfg.getParentDatabase().getParent();

            @SuppressWarnings("unchecked")
            Vector<String> attachments = session.evaluate("@AttachmentNames", docCfg);

            //find the .p12 attachment and load up the bytes
            for (String a : attachments) {
                if (a.endsWith(".p12")) {
                    EmbeddedObject eo = docCfg.getAttachment(a);
                    in = eo.getInputStream();
                    cfg.setApnKeyStore(IOUtils.toByteArray(eo.getInputStream()));
                }
            }

            //set the appId.
            cfg.setAppId(docCfg.getItemValueString("appId"));

            //password used to authenticate to the APN, it is used w/ the .p12 certificate
            cfg.setApnPassword(docCfg.getItemValueString("apnPassword"));

            //set to determine which APN to target (TEST, or PROD)
            cfg.setApnProd(new Boolean(docCfg.getItemValueString("apnProd")));

            //set the app title.
            cfg.setAppTitle(docCfg.getItemValueString("appTitle"));

            //store the messages in the apn.nsf
            cfg.setStoreMessages(new Boolean(docCfg.getItemValueString("storeMessages")));

        } catch (Exception e) {
            logger.log(Level.SEVERE, null, e);
        } finally {
            CloseUtils.close(in);
        }

        return cfg;
    }

    @Override
    public void clearCache() {
        cache.clear();
    }

    @Override
    public AppConfig provideAppConfig(Document docCfg) {
        return this.buildConfig(docCfg);
    }

}