1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /**
36*cdf0e10cSrcweir  * Deleting a resource
37*cdf0e10cSrcweir  */
38*cdf0e10cSrcweir public class ResourceRemover {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir     /**
41*cdf0e10cSrcweir      * Member properties
42*cdf0e10cSrcweir      */
43*cdf0e10cSrcweir     private  Helper   m_helper;
44*cdf0e10cSrcweir     private  String   m_contenturl    = "";
45*cdf0e10cSrcweir     private  com.sun.star.ucb.XContent m_content;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     /**
48*cdf0e10cSrcweir      * Constructor.
49*cdf0e10cSrcweir      *
50*cdf0e10cSrcweir      *@param      String[]   This construtor requires the arguments:
51*cdf0e10cSrcweir      *                          -url=...     (optional)
52*cdf0e10cSrcweir      *                          -workdir=... (optional)
53*cdf0e10cSrcweir      *                       See Help (method printCmdLineUsage()).
54*cdf0e10cSrcweir      *                       Without the arguments a new connection to a
55*cdf0e10cSrcweir      *                       running office cannot created.
56*cdf0e10cSrcweir      *@exception  java.lang.Exception
57*cdf0e10cSrcweir      */
58*cdf0e10cSrcweir     public ResourceRemover( String args[] ) throws java.lang.Exception {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         // Parse arguments
61*cdf0e10cSrcweir         parseArguments( args );
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir         // Init
64*cdf0e10cSrcweir         m_helper       = new Helper( getContentURL() );
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir         // Create UCB content
67*cdf0e10cSrcweir         m_content      = m_helper.createUCBContent();
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     /**
71*cdf0e10cSrcweir      *  Delete resource.
72*cdf0e10cSrcweir      *
73*cdf0e10cSrcweir      *@return     boolean   Returns true if resource successfully deleted, false otherwise
74*cdf0e10cSrcweir      *@exception  com.sun.star.ucb.CommandAbortedException
75*cdf0e10cSrcweir      *@exception  com.sun.star.uno.Exception
76*cdf0e10cSrcweir      */
77*cdf0e10cSrcweir     public boolean deleteResource()
78*cdf0e10cSrcweir         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         boolean result = false;
81*cdf0e10cSrcweir         if ( m_content != null ) {
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
84*cdf0e10cSrcweir             // Destroy a resource physically...
85*cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             Boolean deletePhysically = new Boolean( true );
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir             // Execute command "delete".
90*cdf0e10cSrcweir             m_helper.executeCommand( m_content, "delete", deletePhysically );
91*cdf0e10cSrcweir             result = true;
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir         return result;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     /**
97*cdf0e10cSrcweir      *  Get connect URL.
98*cdf0e10cSrcweir      *
99*cdf0e10cSrcweir      *@return   String    That contains the connect URL
100*cdf0e10cSrcweir      */
101*cdf0e10cSrcweir     public String getContentURL() {
102*cdf0e10cSrcweir         return m_contenturl;
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     /**
106*cdf0e10cSrcweir      * Parse arguments
107*cdf0e10cSrcweir      *
108*cdf0e10cSrcweir      *@param      String[]   Arguments
109*cdf0e10cSrcweir      *@exception  java.lang.Exception
110*cdf0e10cSrcweir      */
111*cdf0e10cSrcweir     public void parseArguments( String[] args ) throws java.lang.Exception {
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         String workdir = "";
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         for ( int i = 0; i < args.length; i++ ) {
116*cdf0e10cSrcweir             if ( args[i].startsWith( "-url=" )) {
117*cdf0e10cSrcweir                 m_contenturl    = args[i].substring( 5 );
118*cdf0e10cSrcweir             } else if ( args[i].startsWith( "-workdir=" )) {
119*cdf0e10cSrcweir                 workdir = args[i].substring( 9 );
120*cdf0e10cSrcweir             } else if ( args[i].startsWith( "-help" ) ||
121*cdf0e10cSrcweir                         args[i].startsWith( "-?" )) {
122*cdf0e10cSrcweir                 printCmdLineUsage();
123*cdf0e10cSrcweir                 System.exit( 0 );
124*cdf0e10cSrcweir             }
125*cdf0e10cSrcweir          }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
128*cdf0e10cSrcweir             m_contenturl = Helper.createTargetDataFile( workdir );
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     /**
133*cdf0e10cSrcweir      * Print the commands options
134*cdf0e10cSrcweir      */
135*cdf0e10cSrcweir     public void printCmdLineUsage() {
136*cdf0e10cSrcweir         System.out.println(
137*cdf0e10cSrcweir             "Usage   : ResourceRemover -url=... -workdir=..." );
138*cdf0e10cSrcweir         System.out.println(
139*cdf0e10cSrcweir             "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
140*cdf0e10cSrcweir         System.out.println(
141*cdf0e10cSrcweir             "\nExample  : -url=file:///temp/MyFile.txt \n" );
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     /**
145*cdf0e10cSrcweir      *  Create a new connection with the specific args to a running office and
146*cdf0e10cSrcweir      *  delete a resource.
147*cdf0e10cSrcweir      *
148*cdf0e10cSrcweir      *@param  String[]   Arguments
149*cdf0e10cSrcweir      */
150*cdf0e10cSrcweir     public static void main ( String args[] ) {
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         System.out.println( "\n" );
153*cdf0e10cSrcweir 		System.out.println(
154*cdf0e10cSrcweir             "-----------------------------------------------------------------" );
155*cdf0e10cSrcweir 		System.out.println(
156*cdf0e10cSrcweir             "ResourceRemover - destroys a resource." );
157*cdf0e10cSrcweir 		System.out.println(
158*cdf0e10cSrcweir             "-----------------------------------------------------------------" );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         try {
161*cdf0e10cSrcweir             ResourceRemover delete = new ResourceRemover( args );
162*cdf0e10cSrcweir             boolean result = delete.deleteResource();
163*cdf0e10cSrcweir             String url = delete.getContentURL();
164*cdf0e10cSrcweir             if ( result )  {
165*cdf0e10cSrcweir                 System.out.println(
166*cdf0e10cSrcweir                         "Delete of resource " + url + " succeeded." );
167*cdf0e10cSrcweir             } else  {
168*cdf0e10cSrcweir                 System.out.println(
169*cdf0e10cSrcweir                         "Delete of resource " + url + " failed." );
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
172*cdf0e10cSrcweir             System.out.println( "Error: " + e );
173*cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
174*cdf0e10cSrcweir             System.out.println( "Error: " + e );
175*cdf0e10cSrcweir         } catch ( java.lang.Exception e ) {
176*cdf0e10cSrcweir             System.out.println( "Error: " + e );
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         System.exit( 0 );
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir }
181