Java tutorial
/* * Copyright (c) 2010 mobiaware.com. * * 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.mobiaware.auction.notify.impl; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import javax.net.ssl.SSLContext; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import com.google.common.eventbus.Subscribe; import com.mobiaware.auction.notify.NotificationService; import com.mobiaware.util.PropertyManager; import com.relayrides.pushy.apns.ApnsEnvironment; import com.relayrides.pushy.apns.PushManager; import com.relayrides.pushy.apns.PushManagerFactory; import com.relayrides.pushy.apns.RejectedNotificationListener; import com.relayrides.pushy.apns.RejectedNotificationReason; import com.relayrides.pushy.apns.util.SimpleApnsPushNotification; public class PushNotificationService implements NotificationService { private static final String NAME = PushNotificationService.class.getSimpleName(); private static final Logger LOG = LoggerFactory.getLogger(NAME); private static volatile PushNotificationService instance = new PushNotificationService(); private PushManager<SimpleApnsPushNotification> _pushManager; public static class PushManagerRejectedNotificationListener implements RejectedNotificationListener<SimpleApnsPushNotification> { @Override public void handleRejectedNotification(final PushManager<? extends SimpleApnsPushNotification> pushManager, final SimpleApnsPushNotification notification, final RejectedNotificationReason reason) { LOG.error("%s was rejected with rejection reason %s\n", notification, reason); } } private PushNotificationService() { PropertyManager pm = new PropertyManager("notification.properties", System.getProperty("NOTIFICATION_CONFIG")); String keystore = pm.getString("apns.keystore"); String password = pm.getString("apns.password"); boolean sandbox = Boolean.parseBoolean(pm.getString("apns.sandbox")); InputStream is = null; try { is = new FileInputStream(keystore); KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(is, password.toCharArray()); ApnsEnvironment environment = sandbox ? ApnsEnvironment.getSandboxEnvironment() : ApnsEnvironment.getProductionEnvironment(); SSLContext context = PushManagerFactory.createDefaultSSLContext(keyStore, password.toCharArray()); PushManagerFactory<SimpleApnsPushNotification> pushManagerFactory = new PushManagerFactory<SimpleApnsPushNotification>( environment, context); _pushManager = pushManagerFactory.buildPushManager(); _pushManager.registerRejectedNotificationListener(new PushManagerRejectedNotificationListener()); } catch (NoSuchAlgorithmException e) { LOG.error(Throwables.getStackTraceAsString(e)); } catch (CertificateException e) { LOG.error(Throwables.getStackTraceAsString(e)); } catch (IOException e) { LOG.error(Throwables.getStackTraceAsString(e)); } catch (KeyStoreException e) { LOG.error(Throwables.getStackTraceAsString(e)); } catch (UnrecoverableKeyException e) { LOG.error(Throwables.getStackTraceAsString(e)); } catch (KeyManagementException e) { LOG.error(Throwables.getStackTraceAsString(e)); } finally { IOUtils.closeQuietly(is); } } public static PushNotificationService getInstance() { return instance; } public PushManager<SimpleApnsPushNotification> getPushManager() { return _pushManager; } @Override public void start() { Preconditions.checkNotNull(getPushManager()); getPushManager().start(); if (LOG.isDebugEnabled()) { LOG.debug("Apple notifications (APNS) started."); } } @Override public void stop() { Preconditions.checkNotNull(getPushManager()); try { getPushManager().shutdown(); } catch (InterruptedException e) { LOG.error(Throwables.getStackTraceAsString(e)); } if (LOG.isDebugEnabled()) { LOG.debug("Apple notifications (APNS) stopped."); } } @Subscribe public void listen(final PushNotification notification) { sendMessage(notification); } private void sendMessage(final PushNotification notification) { Preconditions.checkNotNull(notification); if (LOG.isDebugEnabled()) { LOG.debug("Sending Apple notifications (APNS) [" + notification.getPayload() + "]"); } getPushManager().getQueue().addAll(notification.getNotifications()); } }