Spring Annotation Based Single Class Web Application

Works on Servlet 3.0+ containers.
Better not mix Configuration classes with Controller classes. It is only for simplicity ;)

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
@Configuration
@EnableWebMvc
@Controller
public class SpringWebApp implements WebApplicationInitializer {
 
	@RequestMapping("/")
	@ResponseBody
	public String hello() {
		return "Hello World!";
	}
 
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(SpringWebApp.class);
		ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
		registration.setLoadOnStartup(1);
		registration.addMapping("/");			
	}
}

Complete pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>net.marioosh</groupId>
	<artifactId>spring-annotation-based</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
 
			<!-- Jetty testing server -->
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>9.2.11.v20150529</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<webApp>
						<contextPath>/spring</contextPath>
					</webApp>
				</configuration>
			</plugin>			
		</plugins>
	</build>
</project>
Posted in Java, Spring | Leave a comment

Java 8 Lambda expressions

(parameters) -> expression
(parameters) -> {statements;}

Examples from Java 8 Lambdas: Functional Programming For The Masses

Example 1

ActionListener oneArgument = new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent event) {
    System.out.println("button clicked");
  }
};
ActionListener oneArgument = event -> System.out.println("button clicked");

Example 2

Runnable noArguments = new Runnable() {
  @Override
  void run() {
    System.out.println("Hello World");
  }
};
Runnable noArguments = () -> System.out.println("Hello World");

Example 3

Runnable noArguments = new Runnable() {
  @Override
  void run() {
    System.out.print("Hello");
    System.out.println(" World");
  }
};
Runnable multiStatement = () -> {
  System.out.print("Hello");
  System.out.println(" World");
};

Example 4

BinaryOperator add = new BinaryOperator() {
  @Override
  public Long apply(Long x, Long y) {
    return x + y;
  }
};
BinaryOperator add = (x, y) -> x + y;
 
BinaryOperator addExplicit = (Long x, Long y) -> x + y;
Posted in Java | Leave a comment

Angular starter project

https://github.com/marioosh-net/angular-starter

Posted in Uncategorized | Leave a comment

maven: Deploying .jar to remote Maven repository by ftp

	<build>
		<plugins>
                        <!-- other plugins, skipped -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-deploy-plugin</artifactId>
				<version>2.8.2</version>
			</plugin>
		</plugins>
 
		<extensions>
			<!-- Enabling the use of FTP -->
			<extension>
				<groupId>org.apache.maven.wagon</groupId>
         		<artifactId>wagon-ftp</artifactId>
         		<version>1.0-beta-6</version>
			</extension>
		</extensions>
	</build>
 
	<distributionManagement>
		<repository>
			<id>repo-server</id>
			<url>ftp://192.168.1.253/var/maven</url>
		</repository>
	</distributionManagement>

~/.m2/settings.xml – need login/pass

	<settings>
		<servers>
			<server>
				<id>repo-server</id>
				<username>maven</username>
				<password>***</password>
			</server>
		</servers>
	</settings>

On server (need active ftp daemon working with local user login/passwords):
$ useradd -m maven
$ passwd maven
$ mkdir /var/maven
$ chown -R maven:maven /var/maven

Posted in Uncategorized | Leave a comment

sendmail: send file contents

sendmail someone@somewhere.com < somefile

Posted in Linux, Linux shell, Uncategorized | Leave a comment

Git tags

Tags
git tag -a v1.0 -m 'version 1.0' # add tag
git tag -d v1.0 # remove tag
git push origin :refs/tags/v1.0 # remove remote tag

Posted in git | Leave a comment

Eclipse SWT: javadocs link

http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/index.html

Posted in Uncategorized | Leave a comment

curl: basic auth + json + post

curl --user user:pass -k -v -H "Content-Type: application/json; charset=utf-8" -X POST -d '{"text":"message"}' https://somewhere.com/rest/test

Posted in Uncategorized | Leave a comment

Android – Save / Load state Fragment

public class SomeFragment extends Fragment {
 
    public final static String OBJECT_ATTR = "object";
    Data data;
 
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 
        /**
         * restore
         */
        if(savedInstanceState != null) {
            data = savedInstanceState.getParcelable(OBJECT_ATTR);
        }
 
        /**
         * using data
         */
        ...
    }
 
    /**
     * called when configuration change, but not on getting fragment from back-stack
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        /**
         * save
         */
        super.onSaveInstanceState(outState);
        outState.putParcelable(OBJECT_ATTR, data);
    }
}
Posted in Android, Java | Leave a comment

Android: Install .apk from command line

adb install -r app.apk
Posted in Android | Leave a comment