/**
 * Copyright (C) 2010 Asterios Raptis
 *
 * 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 de.alpharogroup.wicket.components.examples;

import java.io.File;

import lombok.experimental.ExtensionMethod;

import org.apache.log4j.Logger;
import org.apache.wicket.protocol.http.ContextParamWebApplicationFactory;
import org.apache.wicket.protocol.http.WicketFilter;
import org.apache.wicket.util.time.Duration;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;

import de.alpharogroup.file.search.PathFinder;
import de.alpharogroup.jetty9.runner.Jetty9Runner;
import de.alpharogroup.jetty9.runner.config.FilterHolderConfiguration;
import de.alpharogroup.jetty9.runner.config.Jetty9RunConfiguration;
import de.alpharogroup.jetty9.runner.config.ServletContextHandlerConfiguration;
import de.alpharogroup.jetty9.runner.config.ServletHolderConfiguration;
import de.alpharogroup.jetty9.runner.factories.DeploymentManagerFactory;
import de.alpharogroup.jetty9.runner.factories.ServletContextHandlerFactory;
import de.alpharogroup.jetty9.runner.log.LoggerExtensions;
import de.alpharogroup.wicket.components.examples.application.WicketApplication;

@ExtensionMethod(LoggerExtensions.class)
public class StartComponentExamples
{
	public static void main(String[] args) throws Exception
	{
		int sessionTimeout = (int)Duration.minutes(1).seconds();// set timeout to 30min(60sec *
																	// 30min=1800sec)...
		System.setProperty("wicket.configuration", "development");
		String projectname = "jaulp.wicket.components.examples";
		File projectDirectory = PathFinder.getProjectDirectory();
		File webapp;
		if(projectDirectory.getAbsolutePath().endsWith(projectname)) {
			webapp = PathFinder.getRelativePath(projectDirectory, "src", "main",
				"webapp");			
		} else {
			webapp = PathFinder.getRelativePath(projectDirectory, projectname, "src", "main",
				"webapp");
		}
		String filterPath = "/*";
		// Add a file appender to the logger programatically
		// Logger logger = org.apache.log4j.LogManager.getLogger("org.eclipse.jetty");
		Logger.getRootLogger().addFileAppender(LoggerExtensions.newFileAppender("./application.log"));
		ContextHandlerCollection contexts = new ContextHandlerCollection();
		
		ServletContextHandler servletContextHandler = ServletContextHandlerFactory
			.getNewServletContextHandler(ServletContextHandlerConfiguration
				.builder()
				.parent(contexts)
				.filterHolderConfiguration(
					FilterHolderConfiguration
						.builder()
						.filterClass(WicketFilter.class)
						.filterPath(filterPath)
						.initParameter(WicketFilter.FILTER_MAPPING_PARAM, filterPath)
						.initParameter(ContextParamWebApplicationFactory.APP_CLASS_PARAM,
							WicketApplication.class.getName()).build())
				.servletHolderConfiguration(
					ServletHolderConfiguration.builder().servletClass(DefaultServlet.class)
						.pathSpec(filterPath).build()).contextPath("/").webapp(webapp)
				.maxInactiveInterval(sessionTimeout).filterPath(filterPath).build());
		
		DeploymentManager deployer = DeploymentManagerFactory.newDeploymentManager(contexts, webapp.getAbsolutePath(), null);
		Jetty9RunConfiguration config = Jetty9RunConfiguration.builder()
			.servletContextHandler(servletContextHandler)
			.contexts(contexts)
			.deployer(deployer)
			.httpPort(WicketApplication.DEFAULT_HTTP_PORT)
			.httpsPort(WicketApplication.DEFAULT_HTTPS_PORT)
			.keyStorePassword("wicket")
			.keyStorePathResource("/keystore").build();
		Server server = new Server();
		Jetty9Runner.run(server, config);
	}
}

@@ -674,16 +674,44 @@
 io.File;
+%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -1216,16 +1216,68 @@
 ndler;%0A%0A
+import de.alpharogroup.file.delete.DeleteFileUtils;%0A
 import d
@@ -2070,25 +2070,8 @@
 rgs)
- throws Exception
 %0A%09%7B%0A
@@ -2663,16 +2663,318 @@
 %22);%0A%09%09%7D%0A
+%09%09File logfile = new File(projectDirectory, %22application.log%22);%0A%09%09if(logfile.exists()) %7B%0A%09%09%09try %7B%0A%09%09%09%09DeleteFileUtils.delete(logfile);%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09Logger.getRootLogger().error(%22logfile could not deleted.%22, e);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09String absolutePathFromLogfile = logfile.getAbsolutePath();%0A
 %09%09String
@@ -3052,89 +3052,8 @@
 lly%0A
-%09%09// Logger logger = org.apache.log4j.LogManager.getLogger(%22org.eclipse.jetty%22);%0A
 %09%09Lo
@@ -3126,27 +3126,31 @@
 der(
-%22./application.log%22
+absolutePathFromLogfile
 ));%0A

delete obsolete log file on start