1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /**
25cdf0e10cSrcweir  * Deleting a resource
26cdf0e10cSrcweir  */
27cdf0e10cSrcweir public class ResourceRemover {
28cdf0e10cSrcweir 
29cdf0e10cSrcweir     /**
30cdf0e10cSrcweir      * Member properties
31cdf0e10cSrcweir      */
32cdf0e10cSrcweir     private  Helper   m_helper;
33cdf0e10cSrcweir     private  String   m_contenturl    = "";
34cdf0e10cSrcweir     private  com.sun.star.ucb.XContent m_content;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     /**
37cdf0e10cSrcweir      * Constructor.
38cdf0e10cSrcweir      *
39cdf0e10cSrcweir      *@param      String[]   This construtor requires the arguments:
40cdf0e10cSrcweir      *                          -url=...     (optional)
41cdf0e10cSrcweir      *                          -workdir=... (optional)
42cdf0e10cSrcweir      *                       See Help (method printCmdLineUsage()).
43cdf0e10cSrcweir      *                       Without the arguments a new connection to a
44cdf0e10cSrcweir      *                       running office cannot created.
45cdf0e10cSrcweir      *@exception  java.lang.Exception
46cdf0e10cSrcweir      */
ResourceRemover( String args[] )47cdf0e10cSrcweir     public ResourceRemover( String args[] ) throws java.lang.Exception {
48cdf0e10cSrcweir 
49cdf0e10cSrcweir         // Parse arguments
50cdf0e10cSrcweir         parseArguments( args );
51cdf0e10cSrcweir 
52cdf0e10cSrcweir         // Init
53cdf0e10cSrcweir         m_helper       = new Helper( getContentURL() );
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         // Create UCB content
56cdf0e10cSrcweir         m_content      = m_helper.createUCBContent();
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     /**
60cdf0e10cSrcweir      *  Delete resource.
61cdf0e10cSrcweir      *
62cdf0e10cSrcweir      *@return     boolean   Returns true if resource successfully deleted, false otherwise
63cdf0e10cSrcweir      *@exception  com.sun.star.ucb.CommandAbortedException
64cdf0e10cSrcweir      *@exception  com.sun.star.uno.Exception
65cdf0e10cSrcweir      */
deleteResource()66cdf0e10cSrcweir     public boolean deleteResource()
67cdf0e10cSrcweir         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         boolean result = false;
70cdf0e10cSrcweir         if ( m_content != null ) {
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
73cdf0e10cSrcweir             // Destroy a resource physically...
74cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             Boolean deletePhysically = new Boolean( true );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             // Execute command "delete".
79cdf0e10cSrcweir             m_helper.executeCommand( m_content, "delete", deletePhysically );
80cdf0e10cSrcweir             result = true;
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir         return result;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     /**
86cdf0e10cSrcweir      *  Get connect URL.
87cdf0e10cSrcweir      *
88cdf0e10cSrcweir      *@return   String    That contains the connect URL
89cdf0e10cSrcweir      */
getContentURL()90cdf0e10cSrcweir     public String getContentURL() {
91cdf0e10cSrcweir         return m_contenturl;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /**
95cdf0e10cSrcweir      * Parse arguments
96cdf0e10cSrcweir      *
97cdf0e10cSrcweir      *@param      String[]   Arguments
98cdf0e10cSrcweir      *@exception  java.lang.Exception
99cdf0e10cSrcweir      */
parseArguments( String[] args )100cdf0e10cSrcweir     public void parseArguments( String[] args ) throws java.lang.Exception {
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         String workdir = "";
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         for ( int i = 0; i < args.length; i++ ) {
105cdf0e10cSrcweir             if ( args[i].startsWith( "-url=" )) {
106cdf0e10cSrcweir                 m_contenturl    = args[i].substring( 5 );
107cdf0e10cSrcweir             } else if ( args[i].startsWith( "-workdir=" )) {
108cdf0e10cSrcweir                 workdir = args[i].substring( 9 );
109cdf0e10cSrcweir             } else if ( args[i].startsWith( "-help" ) ||
110cdf0e10cSrcweir                         args[i].startsWith( "-?" )) {
111cdf0e10cSrcweir                 printCmdLineUsage();
112cdf0e10cSrcweir                 System.exit( 0 );
113cdf0e10cSrcweir             }
114cdf0e10cSrcweir          }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
117cdf0e10cSrcweir             m_contenturl = Helper.createTargetDataFile( workdir );
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /**
122cdf0e10cSrcweir      * Print the commands options
123cdf0e10cSrcweir      */
printCmdLineUsage()124cdf0e10cSrcweir     public void printCmdLineUsage() {
125cdf0e10cSrcweir         System.out.println(
126cdf0e10cSrcweir             "Usage   : ResourceRemover -url=... -workdir=..." );
127cdf0e10cSrcweir         System.out.println(
128cdf0e10cSrcweir             "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
129cdf0e10cSrcweir         System.out.println(
130cdf0e10cSrcweir             "\nExample  : -url=file:///temp/MyFile.txt \n" );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /**
134cdf0e10cSrcweir      *  Create a new connection with the specific args to a running office and
135cdf0e10cSrcweir      *  delete a resource.
136cdf0e10cSrcweir      *
137cdf0e10cSrcweir      *@param  String[]   Arguments
138cdf0e10cSrcweir      */
main( String args[] )139cdf0e10cSrcweir     public static void main ( String args[] ) {
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         System.out.println( "\n" );
142cdf0e10cSrcweir 		System.out.println(
143cdf0e10cSrcweir             "-----------------------------------------------------------------" );
144cdf0e10cSrcweir 		System.out.println(
145cdf0e10cSrcweir             "ResourceRemover - destroys a resource." );
146cdf0e10cSrcweir 		System.out.println(
147cdf0e10cSrcweir             "-----------------------------------------------------------------" );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         try {
150cdf0e10cSrcweir             ResourceRemover delete = new ResourceRemover( args );
151cdf0e10cSrcweir             boolean result = delete.deleteResource();
152cdf0e10cSrcweir             String url = delete.getContentURL();
153cdf0e10cSrcweir             if ( result )  {
154cdf0e10cSrcweir                 System.out.println(
155cdf0e10cSrcweir                         "Delete of resource " + url + " succeeded." );
156cdf0e10cSrcweir             } else  {
157cdf0e10cSrcweir                 System.out.println(
158cdf0e10cSrcweir                         "Delete of resource " + url + " failed." );
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
161cdf0e10cSrcweir             System.out.println( "Error: " + e );
162cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
163cdf0e10cSrcweir             System.out.println( "Error: " + e );
164cdf0e10cSrcweir         } catch ( java.lang.Exception e ) {
165cdf0e10cSrcweir             System.out.println( "Error: " + e );
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         System.exit( 0 );
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir }
170