001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.weaver.privilizer.example; 020 021import java.util.ArrayList; 022 023import org.apache.commons.weaver.privilizer.Privileged; 024 025public class UsingArgs { 026 027 @Privileged 028 String getProperty(String name) { 029 return System.getProperty(name); 030 } 031 032 @Privileged 033 String[] getProperties(String... names) { 034 if (names == null) { 035 return null; 036 } 037 final ArrayList<String> result = new ArrayList<String>(); 038 // in reality one would delegate to #getProperty to minimize the scope 039 // of the privileged action 040 for (String name : names) { 041 result.add(System.getProperty(name)); 042 } 043 return result.toArray(new String[result.size()]); 044 } 045 046 @Privileged 047 void throwAwayProperty(int first, String middle, char last) { 048 System.getProperty(new StringBuilder().append((char) first).append(middle).append(last).toString()); 049 } 050 051 @Privileged 052 Object assembleAndGetProperty(char first, CharSequence middle, int last) { 053 return System.getProperty(new StringBuilder().append(first).append(middle).append((char) last).toString()); 054 } 055 056 public static class CheckedException1 extends Exception { 057 private static final long serialVersionUID = 1L; 058 } 059 060 public static class CheckedException2 extends Exception { 061 private static final long serialVersionUID = 1L; 062 } 063 064 @Privileged 065 int throwingCheckedException(int which, String propertyToGet) throws CheckedException1, CheckedException2 { 066 System.getProperty(propertyToGet); 067 switch (which) { 068 case 1: 069 throw new CheckedException1(); 070 case 2: 071 throw new CheckedException2(); 072 default: 073 return which; 074 } 075 } 076}