2010年6月18日
#
If you are building standalone application in Java, Maven is your friend when packing your application,
There are two way to let Maven package your application, either as a single jar with all your dependencies jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
One advantage if you choose to do this way is if you need to sign your application jar.
This is needed if you are building a Java Web Start client and you need more access than connecting back to the server.
To read more about have Maven signing your jar read http://maven.apache.org/plugins/maven-jar-plugin/usage.html.
But if you choose to go this way, make sure that all license agreement are shipped with your one single jar.
Another way is to let Maven package your source code only and then referring the dependent jar file from the MANIFEST file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>se.msc.adapter.Main</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("15370720.pdf4"), "utf-16");
LineNumberReader lnr=new LineNumberReader(isr);
String line = null;
while((line=lnr.readLine())!=null){
System.out.println(lnr.getLineNumber()+"\t"+line);
}
}
}
兩個(gè)方法的區(qū)別是資源的定義不同, 一個(gè)主要用于相對(duì)與一個(gè)object取資源,而另一個(gè)用于取相對(duì)于classpath的
資源,用的是絕對(duì)路徑。
在使用Class.getResourceAsStream 時(shí), 資源路徑有兩種方式, 一種以 / 開頭,則這樣的路徑是指定絕對(duì)
路徑, 如果不以 / 開頭, 則路徑是相對(duì)與這個(gè)class所在的包的。
在使用ClassLoader.getResourceAsStream時(shí), 路徑直接使用相對(duì)于classpath的絕對(duì)路徑。
舉例,下面的三個(gè)語(yǔ)句,實(shí)際結(jié)果是一樣的:
com.explorers.Test.class.getResourceAsStream("abc.jpg")
= com.explorers.Test.class.getResourceAsStream("/com/explorers/abc.jpg")
= ClassLoader.getResourceAsStream("com/explorers/abc.jpg")
There's a variety of clients for CAS. The
Java-based clients (JA-SIG, Yale, see JA-SIG
website) typically handle the browser-based client interaction with
CAS very well through ServletFilter implementations.
Now what
about programmatic authentication, i.e. achieving authentication through
non-browser based applications? There exists a CAS
.NET client but I did not manage to find the appropriate Java
implementation. So here goes - it is based on the Apache HttpClient.
In
case I missed any existing implementation achieving the same purpose,
let's look at the bright side: at least now I understand the CAS
protocol :-)
My CAS client works within any application. It uses
the HttpClient and behaves like a browser client as CAS requires cookie
support.
Here's the code:
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
/**
* The CasClient allows users to programmatically login
* to CAS protected services based on the CAS 2 protocol.
* This client behaves like a browser-client in terms of
* cookie handling.<br>
*
* @author Mathias Richter
*/
public class CasClient
{
public static Logger LOG = Logger.getLogger( CasClient.class );
public static final String LOGIN_URL_PART = "login";
public static final String SERVICE_VALIDATE_URL_PART = "serviceValidate";
public static final String TICKET_BEGIN = "ticket=";
private static final String LT_BEGIN = "name="lt" value="";
public static final String CAS_USER_BEGIN = "<cas:user>";
public static final String CAS_USER_END = "</cas:user>";
private HttpClient fClient;
private String fCasUrl;
/**
* Construct a new CasClient.
*
* @param casUrl The base URL of the CAS service to be used.
*/
public CasClient( String casBaseUrl )
{
this( new HttpClient(), casBaseUrl );
}
/**
* Construct a new CasClient which uses the specified HttpClient
* for its HTTP calls.
*
* @param client
* @param casBaseUrl
*/
public CasClient( HttpClient client, String casBaseUrl )
{
fClient = client;
fCasUrl = casBaseUrl;
}
/**
* Authenticate the specified username with the specified password.
* This will not yield any ticket, as no service is authenticated
* against. This wil just set the CAS cookie in this client upon
* successful authentication.
*
* @param username
* @param password
*/
public void authenticate( String username, String password )
{
authenticate( null, username, password );
}
/**
* Validate the specified service ticket against the specified service.
* If the ticket is valid, this will yield the clear text user name
* of the autenticated user.<br>
* Note that each service ticket issued by CAS can be used exactly once
* to validate.
*
* @param serviceUrl
* @param serviceTicket
*
* @return Clear text username of the authenticated user.
*/
public String validate( String serviceUrl, String serviceTicket )
{
String result = null;
PostMethod method = new PostMethod( fCasUrl + SERVICE_VALIDATE_URL_PART );
method.setParameter( "service", serviceUrl );
method.setParameter( "ticket", serviceTicket );
try
{
int statusCode = fClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK)
{
LOG.error( "Could not validate: " + method.getStatusLine() );
method.releaseConnection();
} else
{
result = extractUser( new String( method.getResponseBody() ) );
}
} catch ( Exception x )
{
LOG.error( "Could not validate: " + x.toString () );
x.printStackTrace();
}
method.releaseConnection();
return result;
}
/**
* Authenticate the specified user with the specified password against the
* specified service.
*
* @param serviceUrl May be null. If a url is specified, the authentication will happen against this service, yielding a service ticket which can be validated.
* @param username
* @param password
* @return A valid service ticket, if and only if the specified service URL is not null.
*/
public String authenticate( String serviceUrl, String username, String password )
{
String lt = getLt( serviceUrl );
if ( lt == null )
{
LOG.error( "Cannot retrieve LT from CAS. Aborting authentication for '" + username + "'" );
return null;
}
String result = null;
PostMethod method = new PostMethod( fCasUrl + LOGIN_URL_PART );
if ( serviceUrl != null ) // optional
method.setParameter( "service", serviceUrl );
method.setParameter( "_eventId", "submit" );
method.setParameter("username", username );
method.setParameter("password", password );
method.setParameter("lt", lt );
method.setParameter( "gateway", "true" );
try
{
fClient.executeMethod(method);
if ( serviceUrl == null )
{
if ( extractLt( new String( method.getResponseBody() ) ) != null ) // if CAS does not return a login page with an LT authentication was successful
{
LOG.error( "Authentication for '" + username + "' unsuccessful" );
if ( LOG.isDebugEnabled() )
LOG.debug( "Authentication for '" + username + "' unsuccessful." );
} else
{
if ( LOG.isDebugEnabled() )
LOG.debug( "Authentication for '" + username + "' unsuccessful." );
}
} else
{
Header h = method.getResponseHeader( "Location" );
if ( h != null )
result = extractServiceTicket( h.getValue() );
if ( result == null )
LOG.error( "Authentication for '" + username + "' unsuccessful." );
}
} catch ( Exception x )
{
LOG.error( "Could not authenticate'" + username + "':" + x.toString () );
}
method.releaseConnection();
return result;
}
/**
* Helper method to extract the user name from a "service validate" call to CAS.
*
* @param data Response data.
* @return The clear text username, if it could be extracted, null otherwise.
*/
protected String extractUser( String data )
{
String user = null;
int start = data.indexOf( CAS_USER_BEGIN );
if ( start >= 0 )
{
start += CAS_USER_BEGIN.length();
int end = data.indexOf( CAS_USER_END );
if ( end > start )
user = data.substring( start, end );
else
LOG.warn( "Could not extract username from CAS validation response. Raw data is: '" + data + "'" );
} else
{
LOG.warn( "Could not extract username from CAS validation response. Raw data is: '" + data + "'" );
}
return user;
}
/**
* Helper method to extract the service ticket from a login call to CAS.
*
* @param data Response data.
* @return The service ticket, if it could be extracted, null otherwise.
*/
protected String extractServiceTicket( String data )
{
String serviceTicket = null;
int start = data.indexOf( TICKET_BEGIN );
if ( start > 0 )
{
start += TICKET_BEGIN.length();
serviceTicket = data.substring( start );
}
return serviceTicket;
}
/**
* Helper method to extract the LT from a login form from CAS.
*
* @param data Response data.
* @return The LT, if it could be extracted, null otherwise.
*/
protected String extractLt( String data )
{
String token = null;
int start = data.indexOf( LT_BEGIN );
if ( start < 0 )
{
LOG.error( "Could not obtain LT token from CAS: LT Token not found in response." );
} else
{
start += LT_BEGIN.length();
int end = data.indexOf( """, start );
token = data.substring( start, end );
}
return token;
}
/**
* This method requests the original login form from CAS.
* This form contains an LT, an initial token that must be
* presented to CAS upon sending it an authentication request
* with credentials.<br>
* If a service URL is provided (which is optional), this method
* will post the URL such that CAS authenticates against the
* specified service when a subsequent authentication request is
* sent.
*
* @param serviceUrl
* @return The LT token if it could be extracted from the CAS response.
*/
protected String getLt( String serviceUrl )
{
String lt = null;
HttpMethod method = null;
if ( serviceUrl == null )
method = new GetMethod( fCasUrl + LOGIN_URL_PART );
else
{
method = new PostMethod( fCasUrl + LOGIN_URL_PART );
( ( PostMethod ) method ).setParameter( "service", serviceUrl );
}
try
{
int statusCode = fClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK)
{
LOG.error( "Could not obtain LT token from CAS: " + method.getStatusLine() );
method.releaseConnection();
} else
{
Object o = method.getResponseHeaders() ;
return extractLt( new String( method.getResponseBody() ) );
}
} catch ( Exception x )
{
LOG.error( "Could not obtain LT token from CAS: " + x.toString () );
}
method.releaseConnection();
return lt;
}
}
System.getProperty("line.separator")
html
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
servlet
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setDateHeader("expires", 0);
response.addHeader("P3P","CP=CAO PSA OUR");
原文地址 http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.trak
用vim這么久
了,始終也不知道怎么在vim中使用系統(tǒng)粘貼板,通常要在網(wǎng)上復(fù)制一段代碼都是先gedit打開文件,中鍵粘貼后關(guān)閉,然后再用vim打開編輯,真的不
爽;上次論壇上有人問到了怎么在vim中使用系統(tǒng)粘貼板,印象里回復(fù)很多,有好幾頁(yè)的回復(fù)卻沒有解決問題,今天實(shí)在受不了了又在網(wǎng)上找辦法,竟意外地找到
了,貼出來分享一下。
如果只是想使用系統(tǒng)粘貼板的話直接在輸入模式按Shift+Inset就可以了,下面講一下vim的粘貼板的基礎(chǔ)知識(shí),有興趣的可以看看,
應(yīng)該會(huì)有所收獲的。
vim幫助文檔里與粘貼板有關(guān)的內(nèi)容如下:
- vim有12個(gè)粘貼板,分別是0、1、2、...、9、a、“、+;用:reg命令可以查看各個(gè)粘貼板里的內(nèi)容。在vim中簡(jiǎn)單用y只是復(fù)制到
“(雙引號(hào))粘貼板里,同樣用p粘貼的也是這個(gè)粘貼板里的內(nèi)容;
- 要將vim的內(nèi)容復(fù)制到某個(gè)粘貼板,需要退出編輯模式,進(jìn)入正常模式后,選擇要復(fù)制的內(nèi)容,然后按"Ny完成復(fù)制,其中N為粘
貼板號(hào)(注意是按一下雙引號(hào)然后按粘貼板號(hào)最后按y),例如要把內(nèi)容復(fù)制到粘貼板a,選中內(nèi)容后按"ay就可以了,有兩點(diǎn)需要說明一下:
- “號(hào)粘貼板(臨時(shí)粘貼板)比較特殊,直接按y就復(fù)制到這個(gè)粘貼板中了,直接按p就粘貼這個(gè)粘貼板中的內(nèi)容;
- +號(hào)粘貼板是系統(tǒng)粘貼板,用"+y將內(nèi)容復(fù)制到該粘貼板后可以使用Ctrl+V將其粘貼到其他文檔(如firefox、gedit)
中,同理,要把在其他地方用Ctrl+C或右鍵復(fù)制的內(nèi)容復(fù)制到vim中,需要在正常模式下按"+p;
- 要將vim某個(gè)粘貼板里的內(nèi)容粘貼進(jìn)來,需要退出編輯模式,在正常模式按"Np,其中N為粘貼板號(hào),如上所述,可以按"5p將
5號(hào)粘貼板里的內(nèi)容粘貼進(jìn)來,也可以按"+p將系統(tǒng)全局粘貼板里的內(nèi)容粘貼進(jìn)來。
注意:在我這里,只有vim.gtk或vim.gnome才能使用系統(tǒng)全局粘貼板,默認(rèn)的
vim.basic看不到+號(hào)寄存器。
登錄LINUX系統(tǒng)后,經(jīng)常會(huì)看到"you have mail",卻苦于不知道如何查看,相信菜鳥們都遇到過,偶在網(wǎng)上用“linux
mail"找了很久,但大都是介紹mail服務(wù)器的,黃天總算沒負(fù)有心人,在洪恩在找到一篇介紹基礎(chǔ)的文章,不敢獨(dú)享。
系統(tǒng)提供了用戶
之間通信的郵件系統(tǒng),當(dāng)用戶打開終端注冊(cè)登錄時(shí)發(fā)現(xiàn)系統(tǒng)給出如下信息:
you have mail.
這時(shí)用戶可通過鍵入mail命令讀取信件:
$ mail
mail程序?qū)⒅饌€(gè)顯示用戶的信件,并依照時(shí)間順序,顯示最新的信件。每顯示一段信件,mail都詢問用戶是否要對(duì)該信件作些處理。若用戶回答d,則表示
刪除信件;若僅按回車鍵,表示對(duì)信件不作任何改動(dòng)(信件仍舊保存,下次還可讀這一信件);若回答p,則要求重復(fù)顯示信件;s
filename表示要把信件存入所命名的文件;若回答q,表示要從mail退出。
我們?cè)诒菊碌牡谝粋€(gè)例子中演示了如何寫一封信,作為練習(xí),你可送信件給自己,然后鍵入mail讀取自己發(fā)的信件,看看會(huì)有什么效果。(發(fā)信給自己是一種設(shè)
置備忘錄的方法)。
$mail frank 給自己寫信
subject: test
This is a mail test
CRL-d
EOT
$
$mail 查看信件
“/var/spool/mail/frank:”1 message 1 new
>Nfrank@xteam.xteamlinux.comThu
Mar 25 11:00 13/403 “test”
&
Message 1:
From frank Thu Mar 25 11:00:25 1999/3/25
Received: (fromfrank@localhost)
by xteam.xteamlinux.com(8.8.4/8.8.4)
id LAA05170 for frank;Thu 25 Mar 1999 11:00:25 GMT
Date: Thu,25 Mar 1999 11:00:25 GMT
From:RHS Linux User <frank@xteam.xteamlinux.com>
Message-Id:<199903251142.LAA05170@xteam.xteamlinux.com>
To:frank@xteam.xteamlinux.com
Subject:test
Status:R
This is a mail test
&
mail命令還有很多其它用法,例如發(fā)送事先準(zhǔn)備好的信件,或一次送信給若干人。還可以用其它方法送信件。