// Path: src/main/java/com/webpagebytes/wpbsample/utility/PathUtility.java
// public class PathUtility {
// 
//     public static String safePath(String path)
//     {
//         if (path != null)
//         {
//             return path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/com/webpagebytes/wpbsample/utility/SampleConfigurator.java
// public class SampleConfigurator {
// 
// 	private Properties properties;
// 	private static SampleConfigurator instance;
// 	
// 	private SampleConfigurator() {};
// 	
// 	public static void initialize(String configPath) throws IOException
// 	{
// 		Properties properties = new Properties();
// 		properties.load(new FileInputStream(configPath));
// 		SampleConfigurator instance = new SampleConfigurator();
// 		instance.properties = properties;
// 		SampleConfigurator.instance = instance;
// 	}
// 	public static SampleConfigurator getInstance()
// 	{
// 		return instance;
// 	}
// 	public String getConfig(String key)
// 	{
// 		if (properties.containsKey(key))
// 			return properties.get(key).toString();
// 		else
// 			return "";
// 	}
// 
// }
// 
// Path: src/main/java/com/webpagebytes/wpbsample/utility/SampleFopService.java
// public class SampleFopService {
// 
// 	private static SampleFopService instance;
// 	private FopFactory fopFactory;
// 	private TransformerFactory transformerFactory;
// 	private static volatile Object lock = new Object();
// 	
// 	private SampleFopService()
// 	{
// 		transformerFactory = TransformerFactory.newInstance();
// 		fopFactory = FopFactory.newInstance();
// 	}
// 
// 	public static SampleFopService getInstance()
// 	{
// 		if (instance == null)
// 		{
// 			synchronized (lock)
// 			{
// 				if (null == instance)
// 				{
// 					instance = new SampleFopService();
// 				}
// 			}
// 		}
// 		return instance;
// 	}
// 	
// 	public void getContent(InputStream  is, String mimeType, OutputStream os) throws Exception
// 	{
// 		Source source = null;
// 		try
// 		{	
// 			Transformer transformer = transformerFactory.newTransformer(); // identity transformer
// 			source = new StreamSource(is);		
// 			Fop fop = fopFactory.newFop(mimeType, os);
// 			Result res = new SAXResult(fop.getDefaultHandler());
// 			transformer.transform(source, res);			
// 		} catch (Exception e)
// 		{
// 			throw e;
// 		}
// 	}
// 
// }


import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.JobBuilder.*;
import com.webpagebytes.cms.WPBContentServiceFactory;
import com.webpagebytes.wpbsample.utility.PathUtility;
import com.webpagebytes.wpbsample.utility.SampleConfigurator;
import com.webpagebytes.wpbsample.utility.SampleFopService;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.TriggerBuilder.*;

/*
 *   Copyright 2015 Webpagebytes
 *
 * 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.webpagebytes.wpbsample;






public class WPBSampleContextListener implements ServletContextListener {

	private static final Logger log = Logger.getLogger(WPBSampleContextListener.class.getName());
	
	@Override
	public void contextDestroyed(ServletContextEvent servletContext) {
		
		
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContext) {
		
    	log.log(Level.INFO, "WBPBSampleContextListener context initialized");
    	String sampleConfigPath = servletContext.getServletContext().getInitParameter("sampleConfigurationPath");
    	sampleConfigPath = PathUtility.safePath(sampleConfigPath);
		if (null == sampleConfigPath)
		{
			throw new RuntimeException("cannot get wpbSampleConfigPath value from context patameters");
		}
		try
		{
			log.log(Level.INFO, "config path for sample app: " + sampleConfigPath);
			SampleConfigurator.initialize(sampleConfigPath);
		}catch (IOException e)
		{
			throw new RuntimeException("cannot initialize WPBSampleConfigurator with file " + sampleConfigPath);
		}
        //speed up the creation of SampleFopService instance;

// Path: src/main/java/com/webpagebytes/wpbsample/utility/PathUtility.java
// public class PathUtility {
// 
//     public static String safePath(String path)
//     {
//         if (path != null)
//         {
//             return path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/com/webpagebytes/wpbsample/utility/SampleConfigurator.java
// public class SampleConfigurator {
// 
// 	private Properties properties;
// 	private static SampleConfigurator instance;
// 	
// 	private SampleConfigurator() {};
// 	
// 	public static void initialize(String configPath) throws IOException
// 	{
// 		Properties properties = new Properties();
// 		properties.load(new FileInputStream(configPath));
// 		SampleConfigurator instance = new SampleConfigurator();
// 		instance.properties = properties;
// 		SampleConfigurator.instance = instance;
// 	}
// 	public static SampleConfigurator getInstance()
// 	{
// 		return instance;
// 	}
// 	public String getConfig(String key)
// 	{
// 		if (properties.containsKey(key))
// 			return properties.get(key).toString();
// 		else
// 			return "";
// 	}
// 
// }
// 
// Path: src/main/java/com/webpagebytes/wpbsample/utility/SampleFopService.java
// public class SampleFopService {
// 
// 	private static SampleFopService instance;
// 	private FopFactory fopFactory;
// 	private TransformerFactory transformerFactory;
// 	private static volatile Object lock = new Object();
// 	
// 	private SampleFopService()
// 	{
// 		transformerFactory = TransformerFactory.newInstance();
// 		fopFactory = FopFactory.newInstance();
// 	}
// 
// 	public static SampleFopService getInstance()
// 	{
// 		if (instance == null)
// 		{
// 			synchronized (lock)
// 			{
// 				if (null == instance)
// 				{
// 					instance = new SampleFopService();
// 				}
// 			}
// 		}
// 		return instance;
// 	}
// 	
// 	public void getContent(InputStream  is, String mimeType, OutputStream os) throws Exception
// 	{
// 		Source source = null;
// 		try
// 		{	
// 			Transformer transformer = transformerFactory.newTransformer(); // identity transformer
// 			source = new StreamSource(is);		
// 			Fop fop = fopFactory.newFop(mimeType, os);
// 			Result res = new SAXResult(fop.getDefaultHandler());
// 			transformer.transform(source, res);			
// 		} catch (Exception e)
// 		{
// 			throw e;
// 		}
// 	}
// 
// }


// Path: src/main/java/com/webpagebytes/wpbsample/WPBSampleContextListener.java
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.JobBuilder.*;
import com.webpagebytes.cms.WPBContentServiceFactory;
import com.webpagebytes.wpbsample.utility.PathUtility;
import com.webpagebytes.wpbsample.utility.SampleConfigurator;
import com.webpagebytes.wpbsample.utility.SampleFopService;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.TriggerBuilder.*;

/*
 *   Copyright 2015 Webpagebytes
 *
 * 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.webpagebytes.wpbsample;






public class WPBSampleContextListener implements ServletContextListener {

	private static final Logger log = Logger.getLogger(WPBSampleContextListener.class.getName());
	
	@Override
	public void contextDestroyed(ServletContextEvent servletContext) {
		
		
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContext) {
		
    	log.log(Level.INFO, "WBPBSampleContextListener context initialized");
    	String sampleConfigPath = servletContext.getServletContext().getInitParameter("sampleConfigurationPath");
    	sampleConfigPath = PathUtility.safePath(sampleConfigPath);
		if (null == sampleConfigPath)
		{
			throw new RuntimeException("cannot get wpbSampleConfigPath value from context patameters");
		}
		try
		{
			log.log(Level.INFO, "config path for sample app: " + sampleConfigPath);
			SampleConfigurator.initialize(sampleConfigPath);
		}catch (IOException e)
		{
			throw new RuntimeException("cannot initialize WPBSampleConfigurator with file " + sampleConfigPath);
		}
        //speed up the creation of SampleFopService instance;

		SampleFopService.getInstance();
