java Struts

公式よりDLします。

[vagrant@localhost struts]$ wget http://ftp.kddilabs.jp/infosystems/apache/struts/1.3.10/struts-1.3.10-all.zip
–2018-09-17 09:15:23– http://ftp.kddilabs.jp/infosystems/apache/struts/1.3.10/struts-1.3.10-all.zip
ftp.kddilabs.jp をDNSに問いあわせています… 192.26.91.193, 2001:200:601:10:206:5bff:fef0:466c
ftp.kddilabs.jp|192.26.91.193|:80 に接続しています… 接続しました。
HTTP による接続要求を送信しました、応答を待っています… 200 OK
長さ: 46541251 (44M) [application/zip]
`struts-1.3.10-all.zip’ に保存中

100%[======================================>] 46,541,251 621K/s 時間 77s

2018-09-17 09:16:43 (591 KB/s) – `struts-1.3.10-all.zip’ へ保存完了 [46541251/46541251]

[vagrant@localhost struts]$ ls
struts-1.3.10-all.zip

zipを解凍する。
$ unzip struts-1.3.10-all.zip

[vagrant@localhost struts]$ ls
struts-1.3.10 struts-1.3.10-all.zip

strutus

tomcat

tomcatにデプロイするのか?

Chef Workstation

Chef Workstation
– chef
– knife-solo
– knife configure

[vagrant@localhost chef]$ gem list

*** LOCAL GEMS ***

actioncable (5.0.0.1)
actionmailer (5.0.0.1)
actionpack (5.0.0.1)
actionview (5.0.0.1)
activejob (5.0.0.1)
activemodel (5.0.0.1)
activerecord (5.0.0.1)
activesupport (5.0.0.1)
addressable (2.4.0)
airbrussh (1.3.0)
arel (7.1.4)
backports (3.6.8)
bigdecimal (default: 1.2.8)
builder (3.2.2)
bundler (1.13.6)
byebug (9.0.6)
capistrano (3.11.0)
chef (12.16.42)
chef-config (12.16.42)
chef-zero (5.1.0)
chunky_png (1.3.8)
coffee-rails (4.2.1)
coffee-script (2.4.1)
coffee-script-source (1.10.0)
colorator (1.1.0)
compass (1.0.3)
compass-core (1.0.3)
compass-import-once (1.0.5)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
did_you_mean (1.0.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
ffi-yajl (2.3.0)
forwardable-extended (2.6.0)
fuzzyurl (0.9.0)
globalid (0.3.7)
haml (4.0.7)
hashie (3.4.6)
highline (1.7.8)
httparty (0.13.7)
i18n (0.7.0)
iniparse (1.4.2)
io-console (default: 0.4.5)
ipaddress (0.8.3)
jbuilder (2.6.0)
jekyll (3.3.0)
jekyll-feed (0.8.0)
jekyll-sass-converter (1.4.0)
jekyll-watch (1.5.0)
jquery-rails (4.2.1)
json (default: 1.8.3)
knife-solo (0.6.0)
kramdown (1.12.0)
libv8 (3.16.14.15 x86_64-linux)
libyajl2 (1.2.0)
liquid (3.0.6)
listen (3.0.8)
loofah (2.0.3)
lunchy (0.10.4)
mail (2.6.4)
mercenary (0.3.6)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minima (2.0.0)
minitest (5.9.1, 5.8.3)
mixlib-archive (0.2.0)
mixlib-authentication (1.4.1)
mixlib-cli (1.7.0)
mixlib-config (2.2.4)
mixlib-log (1.7.1)
mixlib-shellout (2.2.7)
multi_json (1.12.1)
multi_xml (0.5.5)
mustache (1.0.3)
net-scp (1.2.1)
net-sftp (2.1.2)
net-ssh (3.2.0)
net-ssh-gateway (1.2.0)
net-ssh-multi (1.2.1)
net-telnet (0.1.1)
nio4r (1.2.1)
nokogiri (1.6.8.1)
ohai (8.21.0)
pathutil (0.14.0)
pg (0.19.0)
plist (3.2.0)
power_assert (0.2.6)
proxifier (1.0.3)
psych (default: 2.0.17)
puma (3.6.0)
rack (2.0.1, 1.6.5, 1.6.4)
rack-protection (1.5.3)
rack-test (0.6.3)
rails (5.0.0.1)
rails-dom-testing (2.0.1)
rails-html-sanitizer (1.0.3)
railties (5.0.0.1)
rake (11.3.0, 10.4.2)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
rdoc (default: 4.2.1)
ref (2.0.0)
rouge (1.11.1)
rspec (3.5.0)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-its (1.2.0)
rspec-mocks (3.5.0)
rspec-support (3.5.0)
rspec_junit_formatter (0.2.3)
rubygems-update (2.6.8)
safe_yaml (1.0.4)
sass (3.4.22)
sass-rails (5.0.6)
serverspec (2.37.2)
sfl (2.3)
sinatra (1.4.7)
sinatra-contrib (1.4.7)
specinfra (2.66.0)
spring (2.0.0)
spring-watcher-listen (2.0.1)
sprockets (3.7.0)
sprockets-rails (3.2.0)
sshkit (1.17.0)
syslog-logger (1.6.8)
systemu (2.6.5)
test-unit (3.1.5)
therubyracer (0.12.2)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5, 2.0.2)
turbolinks (5.0.1)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
uglifier (3.0.3)
uuidtools (2.1.5)
web-console (3.4.0)
websocket-driver (0.6.4)
websocket-extensions (0.1.2)
wmi-lite (1.0.0)

gem listでknife-soloとchefが入っています。

Chef

概要
– インフラをコードで記述するためのツール
– https://www.chef.io/

Ship software faster, safer and better
Chef is the leader in Continuous Automation software, an innovator in application automation and one of the founders of the DevOps movement. Chef works with more than a thousand of the most innovative companies around the world to deliver their vision of digital transformation, providing the practices and platform to deliver software at speed.

Surviving and thriving with digital transformation
Continuous change is inevitable, and so continuous automation is necessary. The digital leaders of the near-future, both individual and corporate, will be those who are able to provide automation for effortless infrastructure, compliance at velocity, and delivery of any app, anywhere.

Unix, Ruby

chef 概念 / y用語

Chef server: Chefサーバーでは送られてきたcookbookを元に、複数あるnodeサーバーに対して一気に指示を送り、cookbook通りの構成が瞬時に整う
Node
Workstation:「インフラの構成をこうしたい」というコードが書かれた「cookbook」を用意

Hash set

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		// HashSet
		HashSet<Integer> sales = new HashSet<>();

		sales.add(10);
		sales.add(20);
		sales.add(30);
		sales.add(10);

		System.out.println(sales.size());

		for(Integer sale: sales){
			System.out.println(sale);
		}

		sales.remove(30);

		for(Integer sale: sales){
			System.out.println(sale);
		}
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
3
20
10
30
20
10

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		// HashMap<String, Integer> sales = new HashMap<>();
		Map<String, Integer> sales = new HashMap<>();

		sales.put("tom", 10);
		sales.put("bob", 20);
		sales.put("steve", 30);

		System.out.println(sales.get("tom"));
		System.out.println(sales.size());

		for (Map.Entry<String, Integer> sale: sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}

		sales.put("tom", 100);
		sales.remove("steve");

		for (Map.Entry<String, Integer> sale : sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}
	}
}
import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9));
		sales
			.stream()
			.filter(e -> e % 3 == 0)
			.map(e -> "(" + e + ")")
			.forEach(System.out::println);

		System.out.println();
	}
}
import java.time.*;
import java.time.format.DateTimeFormatter;

public class MyApp {

	public static void main(String[] args){
		LocalDateTime d = LocalDateTime.now();
		System.out.println(d.getYear());
		System.out.println(d.getMonth());
		System.out.println(d.getMonth().getValue());
		System.out.println(d.plusMonths(2).minusDays(3));
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy!MM!dd!");
		System.out.println(d.format(dtf));
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
2018
SEPTEMBER
9
2018-11-13T23:12:54.812
2018!09!16!

スレッド

class MyRunnable implements Runnable{
@Override
public void run(){
for (int i = 0; i < 500; i++){ System.out.print('*'); } } } public class MyApp { public static void main(String[] args){ MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); for (int i = 0; i < 500; i++){ System.out.print('.'); } } } [/java] [vagrant@localhost java]$ java MyApp ..............................................................................................................................................******************************************************************************...................................................................................................................................................**********************************************************************************************************************************************************************************************************************************************...................................................................................................................................................................................................................**************************************************************************************************************************************************************************************** String class [java] public class MyApp { public static void main(String[] args){ String s = "abcdef"; System.out.println(s.length()); System.out.println(s.substring(2, 5)); System.out.println(s.replaceAll("ab", "AB")); String s1 = "ab"; String s2 = "ab"; if (s1.equals(s2)){ System.out.println("same!"); } String ss1 = new String("ab"); String ss2 = new String("ab"); if(ss1 == ss2){ System.out.println("same!same!same!"); } } } [/java] [java] public class MyApp { public static void main(String[] args){ int score = 50; double height = 165.8; String name = "yokoi"; System.out.printf("name: %s, score: %d, height: %f\n", name, score, height); System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height); } } [/java] [java] public class MyApp { public static void main(String[] args){ double d = 53.234; System.out.println(Math.ceil(d)); System.out.println(Math.floor(d)); System.out.println(Math.round(d)); System.out.println(Math.PI); } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 54.0 53.0 53 3.141592653589793 Random処理 [java] Random r = new Random(); System.out.println(r.nextDouble()); System.out.println(r.nextInt(100)); System.out.println(r.nextBoolean()); [/java] [java] import java.util.*; public class MyApp { public static void main(String[] args){ // ArrayList sales = new ArrayList<>();
List sales = new ArrayList<>();

sales.add(10);
sales.add(20);
sales.add(30);

for(int i = 0; i < sales.size(); i++){ System.out.println(sales.get(i)); } sales.set(0, 100); sales.remove(2); for(Integer sale : sales){ System.out.println(sale); } } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 10 20 30 100 20

イニシャライザー

class User {
	private String name;
	private static int count;

	static {
		User.count = 0;
		System.out.println("Static initializer");
	}

	public User(String name){
		this.name = name;
		User.count++;
		System.out.println("Constructor initializer");
	}

	{
		System.out.println("Instance initializer");
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Static initializer
# of instances: 0
Instance initializer
Constructor initializer
# of instances: 1
Instance initializer
Constructor initializer
# of instances: 2

final は継承できない。

final class User {
	protected String name;
	private static final double VERSION = 1.1;

	static {
		this.name = name;
		User.VERSION = 1.2;
	}

	public final void sayHi{
		System.out.println("Hi! " + this.name);
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

抽象クラス

// 抽象クラス -> 具象クラス

abstract class User {
	public abstract void sayHi(); // 抽象メソッド
}

class JapaneseUser extends User {
	@Override
	public void sayHi(){
		System.out.println("こんにちは!");
	}
}

class AmericanUser extends User {
	@Override
	public void sayHi(){
		System.out.println("Hi");
	}
}

public class MyApp {

	public static void main(String[] args){
		AmericanUser tom = new AmericanUser();
		JapaneseUser aki = new JapaneseUser();
		tom.sayHi();
		aki.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi
こんにちは!

interface

interface Printable{
	// 定数
	double VERSION = 1.2;
	// 抽象メソッド
	void print();
	// default メソッド
	public default void getInfo(){
		System.out.println("I/F ver. " + Printable.VERSION);
	} 
	// static メソッド

} 

class User implements Printable {
	@Override
	public void print(){
		System.out.println("Now printing user profile...");
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User();
		tom.print();
		tom.getInfo();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Now printing user profile…
I/F ver. 1.2

列挙型

enum Result {
	SUCCESS,
	ERROR,

}


public class MyApp {

	public static void main(String[] args){
		Result res;

		res = Result.ERROR;

		switch (res){
			case SUCCESS:
				System.out.println("OK!");
				System.out.println(res.ordinal());
			break;
			case ERROR:
				System.out.println("NG!");
				System.out.println(res.ordinal());
				break;
		}

		
	}
}

[vagrant@localhost java]$ java MyApp
NG!
1

例外処理

public class MyApp {

	public static void div(int a, int b){
		System.out.println(a / b);
	}

	public static void main(String[] args){
		div(3, 0);
	}
}

[vagrant@localhost java]$ java MyApp
Exception in thread “main” java.lang.ArithmeticException: / by zero
at MyApp.div(MyApp.java:13)
at MyApp.main(MyApp.java:17)

例外処理

class MyException extends Exception {
	public MyException(String s){
		super(s);
	}
}

public class MyApp {

	public static void div(int a, int b){

		try {
			if (b < 0){
				throw new MyException("not minux!");
			}
			System.out.println(a / b);
		} catch (ArithmeticException e){
			System.err.println(e.getMessage());
		} catch(MyException e){
			System.err.println(e.getMessage());
		}		
	}
	public static void main(String&#91;&#93; args){
		div(3, 0);
		div(5, -2);
	}
}
&#91;/java&#93;

Wrapper Class
&#91;java&#93;
public class MyApp {

	public static void main(String&#91;&#93; args){
		// Integer i = new Integer(32);
		// int n = i.intValue();
		Integer i = 32; // auto boxing
		i = null;
		int n = i; // auto unboxing
		System.out.println();
	}
}
&#91;/java&#93;

&#91;vagrant@localhost java&#93;$ javac MyApp.java
&#91;vagrant@localhost java&#93;$ java MyApp
Exception in thread "main" java.lang.NullPointerException
        at MyApp.main(MyApp.java:14)

型を後程指定するジェネリック
&#91;java&#93;
class MyData<T> {
	public void getThree(T x){
		System.out.println(x);
		System.out.println(x);
		System.out.println(x);
	}
}

public class MyApp {

	public static void main(String[] args){
		MyData<Integer> i = new MyData<>();
		i.getThree(32);
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
32
32
32

パッケージをコンパイルしよう

インポートって、要するに違うファイルからの読み込みね。すっかり忘れてる or スルーしてた。

package jp.hpscript.myapp;
import jp.hpscript.myapp.model.User;
import jp.hpscript.myapp.model.AdminUser;

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		// System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		// System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}
package jp.hpscript.myapp.model;

public class User {
	protected String name;

	public User(String name){
		this.name = name;
	}

	public void sayHi(){
		System.out.println("hi!" + this.name);
	}
}
package jp.hpscript.myapp.model;

public class AdminUser extends User {
	public AdminUser(String name){
		super(name);
	}

	public void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override public void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

[vagrant@localhost java]$ javac jp/hpscript/myapp/MyApp.java
[vagrant@localhost java]$ java jp.hpscript.myapp.MyApp
hi!Tom
[admin]hi!bob
hello!bob

setter, getter

class User {
	private String name;
	private int score;

	public User(String name, int score){
		this.name = name;
		this.score = score;
	}

	public int getScore(){
		return this.score;
	}

	public void setScore(int score){
		if(score > 0){
			this.score = score;
		}	
	}
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom", 65);
		tom.setScore(85);
		tom.setScore(-22);
		System.out.println(tom.getScore());
	}
}
// static

class User {
	private String name;
	private static int count = 0;

	public User(String name){
		this.name = name;
		User.count++;
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {

	public static void main(String[] args){
		User.getInfo();
		User tom = new User("Tom");
		User.getInfo();
		User john = new User("John");
		User.getInfo();
	}
}

java method

public static void sayHi(){
		System.out.println("Hi!");
	}

	public static void main(String[] args){
		sayHi();
	}
public static void sayHi(String name){
		System.out.println("Hi!" + name);
	}

	public static void main(String[] args){
		sayHi("Tom");
		sayHi("Bob");
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Tom
Hi!Bob

public static String sayHi(String name){
		return "Hi!" + name;
	}

	public static void main(String[] args){
		String msg = sayHi("Steve");
		System.out.println(msg);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Hi!Steve

classを使ってみよう

class User {
	String name = "Me!";

	void sayHi(){
		System.out.println("hi!");
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User();

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Me!
hi!

なるほど。

コンストラクタ

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}

}

public class MyApp {

	public static void main(String[] args){
		User tom;
		tom = new User("Tom");

		System.out.println(tom.name);
		tom.sayHi();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom

なるほど、何故か急に分かってきた。
classの継承

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

@override 継承した時に親クラスのメソッドを使う

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("hi!" + this.name);
	}
}

class AdminUser extends User {
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("hello!" + this.name);
	}
	@Override void sayHi(){
		System.out.println("[admin]hi!" + this.name);
	}	
}

public class MyApp {

	public static void main(String[] args){
		User tom = new User("Tom");
		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHi();
		bob.sayHello();
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
Tom
hi!Tom
bob
[admin]hi!bob
hello!bob
ほうほう

java 基礎

public static void main(String[] args){
		int[] sales;
		sales = new int[3];

		sales[0] = 100;
		sales[1] = 200;
		sales[2] = 300;
		System.out.println(sales[1]);
		
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
200

こうも書ける

public static void main(String[] args){
		int[] sales;
		sales = new int[]{200, 240, 280};

		System.out.println(sales[1]);

	}

forと組み合わせる。

public static void main(String[] args){

int[] sales = {800, 540, 320};

for(int i = 0; i < 3; i++){ System.out.println(sales[i]); } } [/java] (int sale: sales)という書き方 [java] public static void main(String[] args){ int[] sales = {800, 540, 320}; for(int sale: sales){ System.out.println(sale); } } [/java] 基本データ型と参照型 [java] public static void main(String[] args){ int x = 10; int y = x; y = 5; System.out.println(x); System.out.println(y); } [/java] [java] int[] a = {3, 5, 7}; int[] b = a; b[1] = 8; System.out.println(a[1]); System.out.println(b[1]); [/java] 配列の場合はメモリ領域が入る。 [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 8 8 文字列は別の領域に割り当てる。 [java] public static void main(String[] args){ String s = "hello"; String t = s; t = "world"; System.out.println(s); System.out.println(t); } [/java]

javaをもう一回やろう

キャストする

public static void main(String[] args){
		double d = 52343.231;
		int i = (int)d;
		System.out.println(i);
	}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java Myapp
エラー: メイン・クラスMyappが見つからなかったかロードできませんでした
[vagrant@localhost java]$ java MyApp
52343

if文

public static void main(String[] args){
		int score = 85;
		if(score > 80){
			System.out.println("great");	
		}			
	}
public static void main(String[] args){
		int score = 85;
		String msg = score > 80 ? "great" : "soso..!";
		System.out.println(msg);
	}

switch

public static void main(String[] args){
		String signal = "red";
		switch(signal) {
			case "red":
				System.out.println("stop!");
				break;
			case "blue":
				System.out.println("go");
				break;
			case "yello":
				System.out.println("caution");
				break;
			default:
				System.out.println("wrong signal!");
				break;
		}
	}

public static void main(String[] args){
int i = 0;
while (i < 10){ System.out.println(i); i++; } } [/java] [vagrant@localhost java]$ java MyApp 0 1 2 3 4 5 6 7 8 9 こうも行ける。 [java] public static void main(String[] args){ int i = 0; do { System.out.println(i); i++; } while (i < 10); } [/java] [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ System.out.println(i); } } [/java] break, continue [java] public static void main(String[] args){ for (int i = 0; i < 10; i++){ if (i==5){ continue; } System.out.println(i); } } [/java] うおおおおおおお、短期間で詰め込んでるな