Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

2010-05-01から1ヶ月間の記事一覧

Javaでサムネイル画像のような縮小画像を作る

意外にもメジャーなライブラリはないっぽい。JDKのImageIOを使う。 import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; impo…

linuxのTomcatにJVM引数を渡す

$ sudo vi /etc/sysconfig/tomcat6 or $ sudo vi /etc/tomcat6/tomcat6.conf で CATALINA_OPTS="-Xms512m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m"を設定する。どちらの設定ファイルが優先されるかはディストリビューション依存?らしい。 僕は/e…

Spring + DBCP + MySQLで接続が閉じたコネクションを利用して例外が発生することを防ぐ

BasicDataSourceのvalidationQueryプロパティに検証用SQLを設定する。Spring設定ファイル。

Mavenで${basedir}/src/main/webappディレクトリにあるファイルをWARから除外する

パスがわからなくてはまった。 <project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes>*.hoge</warSourceExcludes> </configuration> </plugin> </plugins> </build> </project> カレントが${basedir}/src/main/webappらしい。

MavenでWebアプリケーションを作成するときのアーキタイプ

よく使うだろうからこれだけピックアップ。 mvn archetype:create -DgroupId=xxx.xxxxx -DartifactId=xxxxx -DarchetypeArtifactId=maven-archetype-webapp

MavenでWARにパッケージするときに特定のファイルを除外する

pom.xmlに次のように記述する。 <project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes>**/*.xml</warSourceExcludes> </configuration> </plugin> </plugins> </build> </project>これだとXMLファイルを除外する。

SpringでAOPに複数のポイントカットを指定する

<aop:config> <aop:aspect ref="interceptor"> <aop:pointcut expression="execution(* xxx.xxx.*.update(..)) || execution(* xxx.xxx.*.add(..))" id="pointCut" /> <aop:around pointcut-ref="pointCut" method="aopMethod" /> </aop:aspect> </aop:config>

MacでMySQLにファイルからレコード取り込み

mac

load data infile '/Users/jyukutyo/item.csv' into table item fields terminated by ',' enclosed by '"' lines terminated by '\n';

Jackson JSONで出力日付フォーマットを指定する

Ismail Seyfi's Blog: How to control Date formatting when Jackson JSON Processor is used with Spring 3.0 import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.…

Mavenでコンパイルの文字コードを指定する

Windowsでmvnしたら文字コードのエラーが出たので。 <build> <finalName>xxx</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>

yumでTomcat 6をインストールする

RHELではデフォルトのリポジトリにTomcat 6はない(Tomcat 5.5まで)。とりあえずJDKは6にする。OpenJDK版にした(Sun版だと動作しないかも)。 # yum install java-1.6.0-openjdk-develリポジトリにJPackageを追加してyum installする。 # cd /etc/yum.repos.…

Google Collection Library

commons collectionsは下位互換のためgenericsには対応してないので、Google Collection Libraryを使った。APIの感じはほとんど一緒。 Lists.transform(list, new Function<F, T>() { @Override public T apply(F from) { T to = new T(); return to; } }); 個人的</f,>…

Spring + JacksonでJSONにするとenumはname()の値になる

enumはname()の値がJSONになるけど、enumの定義から考えるとtoString()じゃないのかなと思ったら同じことを考えてJIRAに登録してる人がいた。[#JACKSON-212] use toString() instead of name() for enum type - jira.codehaus.orgけど、まだfixされてないし…

Spring MVCのMappingJacksonJsonViewでマーシャルしないプロパティを設定する

@JsonIgnoreを使用する。でも、 @JsonIgnore private String data; だとマーシャルされる。 @JsonIgnore public String getData() { return data; } のようのgetterにつける。

MacでConsolasをフォントに使うには

mac

Office for Macをインストールしていれば使えるけど、そうでないときはMac OSXでconsolasを使う方法 | KDF Memoを参考にするとできるよ。

プログラミングにおけるフォント

Consolas 13pt が最高と思った。

Macのhosts

mac

sudo vi /private/etc/hosts

Tomcat6を使ってMavenでJSTL 1.2をリポジトリからダウンロードして作ったアプリをデプロイするときのもろもろ

JSTL 1.2は標準のリポジトリに存在しない。Java.netのGlassFishのリポジトリから取得する。ちなみにApache TaglibsのJSTLは開発が終了しているので注意する。 <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/maven-v4_0_0.xsd"> </project>

Tomcatのレルムを独自に実装して認証する

どこかの海外blogから。パスワードを暗号化してデータベースに登録するとき、暗号化ライブラリを利用するとTomcatの認証がそのままでは使えないので、レルムを実装する。たとえばJasyptを使って暗号化しているとき、TomcatのRealmBaseの継承階層を利用して独…

Spring MVCでJSON形式のレスポンスを返す

org.springframework.web.servlet.view.json.MappingJacksonJsonViewクラスを使う。Jacksonというライブラリを変換に使うため、JARファイルを追加しなければClassNotFoundExceptionになる。 jackson-core-asl-1.5.2.jar jackson-mapper-asl-1.5.2.jar ちなみ…

Spring MVCでHTTPステータスコードを返す

@ResponseStatusアノテーションを使うだけ。 @ResponseStatus(HttpStatus.OK) @RequestMapping(method = { RequestMethod.POST }) public void methodName(@RequestParam("Id") Integer id) { ... }

Spring MVCでリクエストパラメータを日付型に変換してControllerに渡す

だれかの海外blogから。Controllerにこういうメソッドがあるとして、Spring MVCではリクエストパラメータをDateに変換してくれない。 @RequestMapping(method = { RequestMethod.POST }) public MappingJacksonJsonView methodName(@RequestParam("date") Da…

SpringですべてのメソッドにAOPでDebugInterceptorを適用する

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor" /> <aop:config> <aop:advisor advice-ref="debugInterceptor" pointcut="execution(* .*(..))" /> </aop:config></bean>

TomcatでのJNDIのデータソース

<Context> <Resource name="jdbc/xxx" auth="Container" type="javax.sql.DataSource" username="xxxxx" password="xxxxx" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/xxxxxx" /> </Context>

メソッドチェインするときのeclipseフォーマット設定

Java - Formatter →Qualified invocations →→Line wrapping policy →→→Wrap all elements, every element on a new line

MavenのTips

settings.xmlで個人設定をする。-DdownloadSources=trueを常に追加するようにしたければ要素に追加する。 <settings> <profiles> <profile> <id>xxx</id> <properties> <downloadSources>true</downloadSources> </properties> </profile> </profiles> <activeProfiles> <activeProfile>xxx</activeProfile> </activeProfiles> </settings> pom.xmlに記述すると個人で…

MacPortsでMySQLをインストールおさらい。

MacPortsで入れたかったから。 MacPortsがない人はまずインストールしてパスに追加する。マンパスも。 $ vi .bash_profile export PATH=opt/local/bin:/opt/local/sbin:$PATH export MANPATH=/opt/local/man:$MANPATH$ sudo port install mysql5 The MySQL c…

Apache-Tomcatの連携おさらい

# yum install tomcat5 tomcat5-admin-webapps tomcat5-webappsTomcatのconf/server.xmlを編集して8080番ポートを使用しないようにする。

Mavenでコンパイラのバージョンを指定する

pom.xmlに記述する。 <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin></plugins>

SSHの鍵を作成してパスワードログインを無効にする

こういうインフラ関係のことは苦手です。。。クライアントマシンで鍵を生成する。-tは鍵の種類を指定するオプション。SSHプロトコル2なら「rsa」を指定する。生成した公開鍵をscpコマンドでサーバーにアップロードする。 $ ssh-keygen -t rsa $ scp ~/.ssh/i…