を提出必要含めJavaの「プロパティ」ファイルの内容、文書化するための十分に確立アプローチがあります:利用できる文書化するためのベスト・プラクティスは、/ Javaプロパティは、内容
指定されたキーの予想データタイプ/内容を指定- は、
- アプリケーションが現在のキーの意味
の説明を提供する
私は[EDIT:関連]
...何私が探していることはプロパティファイルについて "のgetopt" と同等であると思い
を提出必要含めJavaの「プロパティ」ファイルの内容、文書化するための十分に確立アプローチがあります:利用できる文書化するためのベスト・プラクティスは、/ Javaプロパティは、内容
指定されたキーの予想データタイプ/内容を指定の説明を提供する
私は[EDIT:関連]
...何私が探していることはプロパティファイルについて "のgetopt" と同等であると思い
私はそれを行う標準的な方法を見たことがありません。私はおそらく、どうなることは次のとおりです。
あなたが対処しやすいかもしれない別の方法で情報を管理できることを除いては、あなたが手でやっている以上のものを得ることはありません - 例えば、あなたが吐き出すプログラムを持つことができますそれは潜在的にあなたが必要とするプログラム的なアクセスを与えるでしょうが、それはあなた自身のロールになります。
これは、あまりにも多くの利益を得るにはあまりにも多くの作業が必要な場合があります(そのため、そこには明らかなものがありません)。
あなたが見たいコメントの種類を指定することができれば、私が退屈したら何かを書くことができます:-)(これは私が楽しいためにやっていることのようなものです、 )。
よろしくお願いします...退屈しました...
import java.util.Properties;
public class Main
{
public static void main(String[] args)
{
final Properties properties;
final PropertiesVerifier verifier;
properties = new Properties();
properties.put("property.one", "true");
properties.put("property.two", "false");
// properties.put("property.three", "5");
verifier = new PropertiesVerifier(
new PropertiesVerifier.PropertyInfo[]
{
new PropertiesVerifier.PropertyInfo("property.one",
true,
Boolean.class),
new PropertiesVerifier.PropertyInfo("property.two",
false,
Boolean.class),
// new PropertiesVerifier.PropertyInfo("property.three",
// true,
// Boolean.class),
});
System.out.println(verifier.isKnown("property.one"));
System.out.println(verifier.isKnown("property.two"));
System.out.println(verifier.isKnown("property.three"));
System.out.println(verifier.isRequired("property.one"));
System.out.println(verifier.isRequired("property.two"));
System.out.println(verifier.isRequired("property.three"));
System.out.println(verifier.isOptional("property.one"));
System.out.println(verifier.isOptional("property.two"));
System.out.println(verifier.isOptional("property.three"));
System.out.println(verifier.getType("property.one"));
System.out.println(verifier.getType("property.two"));
// System.out.println(verifier.getType("property.tthree"));
System.out.println(verifier.isValid("property.one", "true"));
System.out.println(verifier.isValid("property.two", "false"));
// System.out.println(verifier.isValid("property.tthree", "5"));
verifier.verifyProperties(properties);
}
}
一つの簡単な方法がありますあなたのプロジェクトをサンプルプロパティファイルで配布する私のプロジェクトはsvnに "build.properties.example"というプロパティを持ち、必要に応じてコメントを付けました。ローカルに正しいプロパティはsvnには入りません。
あなたは「getopt」と言いましたが、あなたは本当にcmdの行引数を考えているのでしょうか?特定のプロパティを必要とする「メイン」がある場合、私は通常、引数が正しくないか "-h"を出力する "useage"メッセージに関連する命令を入れます。
あなたの最初の段落は、彼が彼がすでにやっていることをするように指示します。あなたの2番目のパラグラフは質問には関係ありません(彼は彼がプロパティファイルについて話していることを明確に述べています)。 –
さて、実際には、彼は既定のファイルではなく、「死んだ」例で読むことを話していたようです。この質問は、わかりやすくするために少し編集されています。 –
あなたはApache Commons Configurationパッケージ内の機能の一部を使用することができます:ここに
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
public class PropertiesVerifier
{
private final Map<String, PropertyInfo> optionalInfo;
private final Map<String, PropertyInfo> requiredInfo;
{
optionalInfo = new HashMap<String, PropertyInfo>();
requiredInfo = new HashMap<String, PropertyInfo>();
}
public PropertiesVerifier(final PropertyInfo[] infos)
{
for(final PropertyInfo info : infos)
{
final Map<String, PropertyInfo> infoMap;
if(info.isRequired())
{
infoMap = requiredInfo;
}
else
{
infoMap = optionalInfo;
}
infoMap.put(info.getName(), info);
}
}
public void verifyProperties(final Properties properties)
{
for(final Entry<Object, Object> property : properties.entrySet())
{
final String key;
final String value;
key = (String)property.getKey();
value = (String)property.getValue();
if(!(isValid(key, value)))
{
throw new IllegalArgumentException(value + " is not valid for: " + key);
}
}
}
public boolean isRequired(final String key)
{
return (requiredInfo.get(key) != null);
}
public boolean isOptional(final String key)
{
return (optionalInfo.get(key) != null);
}
public boolean isKnown(final String key)
{
return (isRequired(key) || isOptional(key));
}
public Class getType(final String key)
{
final PropertyInfo info;
info = getPropertyInfoFor(key);
return (info.getType());
}
public boolean isValid(final String key,
final String value)
{
final PropertyInfo info;
info = getPropertyInfoFor(key);
return (info.verify(value));
}
private PropertyInfo getPropertyInfoFor(final String key)
{
PropertyInfo info;
info = requiredInfo.get(key);
if(info == null)
{
info = optionalInfo.get(key);
if(info == null)
{
// should be a better exception maybe... depends on how you
// want to deal with it
throw new IllegalArgumentException(key + "
is not a valid property name");
}
}
return (info);
}
protected final static class PropertyInfo
{
private final String name;
private final boolean required;
private final Class clazz;
private final Verifier verifier;
protected PropertyInfo(final String nm,
final boolean mandatory,
final Class c)
{
this(nm, mandatory, c, getDefaultVerifier(c));
}
protected PropertyInfo(final String nm,
final boolean mandatory,
final Class c,
final Verifier v)
{
// check for null
name = nm;
required = mandatory;
clazz = c;
verifier = v;
}
@Override
public int hashCode()
{
return (getName().hashCode());
}
@Override
public boolean equals(final Object o)
{
final boolean retVal;
if(o instanceof PropertyInfo)
{
final PropertyInfo other;
other = (PropertyInfo)o;
retVal = getName().equals(other.getName());
}
else
{
retVal = false;
}
return (retVal);
}
public boolean verify(final String value)
{
return (verifier.verify(value));
}
public String getName()
{
return (name);
}
public boolean isRequired()
{
return (required);
}
public Class getType()
{
return (clazz);
}
}
private static Verifier getDefaultVerifier(final Class clazz)
{
final Verifier verifier;
if(clazz.equals(Boolean.class))
{
// shoudl use a singleton to save space...
verifier = new BooleanVerifier();
}
else
{
throw new IllegalArgumentException("Unknown property type: " +
clazz.getCanonicalName());
}
return (verifier);
}
public static interface Verifier
{
boolean verify(final String value);
}
public static class BooleanVerifier
implements Verifier
{
public boolean verify(final String value)
{
final boolean retVal;
if(value.equalsIgnoreCase("true") ||
value.equalsIgnoreCase("false"))
{
retVal = true;
}
else
{
retVal = false;
}
return (retVal);
}
}
}
そして、それのための簡単なテスト:-)少なくともスタートで何かがあります。少なくともあなたのプロパティへの型アクセスを提供します。
伝統的なJavaプロパティーファイルには規則のみがあります。私が見たことのあるものには、あなたが言ったように、プロパティファイルの例が含まれています。もう一つは、デフォルトの設定にすべてのプロパティを提供することですが、コメントアウトしています。
本当に何かを必要としたい場合は、プロパティファイルを探していない可能性があります。 XML構成ファイルを使用し、データ型と要件を持つスキーマを指定できます。あなたはjaxbを使ってスキーマをjavaにコンパイルし、それを私のように読むことができます。検証によって、必要なプロパティが存在することを確認できます。
アプリケーションを実行すると、ファイル内のプロパティを読み取り、解析し、検証することができます。あなたは絶対にプロパティをベースにしていなければならず、xmlに行きたくはありませんでしたが、この解析が必要でした。含まれる可能性のある各プロパティー、そのタイプ、および必須かどうかを示す2次プロパティー・ファイルを作成することができます。次に、検証するファイルと検証スキーマのようなプロパティファイルを取り込むプロパティファイルの検証ツールを記述する必要があります。何かのように
#list of required properties
required=prop1,prop2,prop3
#all properties and their types
prop1.type=Integer
prop2.type=String
私はすべてのApache Configurationパッケージを調べていませんが、このような便利なユーティリティがよくあります。私はあなたがそこでこれを単純化する何かを見つけることができれば驚かないでしょう。
チェックアウトする別のオプションは、OWNERというプロジェクトです。そこで、タイプと注釈を使用して、アプリケーション内の構成オブジェクトとして機能するインターフェースを定義します。次に、OWNERは正しいProperties
ファイルの検索と解析を行います。したがって、あなたのインタフェース用にjavadocと書いて、それをドキュメントとして使用することができます。
私の答えは、あなたがそれを始められるべきコードを含めるように更新されました。 – TofuBeer
うわー、私はちょうど3つのupvotesと、これについて "人気のある質問"バッジを持って...奇妙な...私はまだそれに満足のいく解決策を取り組んでいます。 – andersoj