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 025 026 027public abstract class StaticUsingArgs { 028 029 private StaticUsingArgs() { 030 } 031 032 @Privileged 033 static String getProperty(String name) { 034 return System.getProperty(name); 035 } 036 037 @Privileged 038 static String[] getProperties(String... names) { 039 if (names == null) { 040 return null; 041 } 042 final ArrayList<String> result = new ArrayList<String>(); 043 // in reality one would delegate to #getProperty to minimize the scope 044 // of the privileged action 045 for (String name : names) { 046 result.add(System.getProperty(name)); 047 } 048 return result.toArray(new String[result.size()]); 049 } 050 051 @Privileged 052 static void throwAwayProperty(int first, String middle, char last) { 053 System.getProperty(new StringBuilder().append((char) first).append(middle).append(last).toString()); 054 } 055 056 @Privileged 057 static Object assembleAndGetProperty(char first, CharSequence middle, int last) { 058 return System.getProperty(new StringBuilder().append(first).append(middle).append((char) last).toString()); 059 } 060 061 public static class CheckedException1 extends Exception { 062 private static final long serialVersionUID = 1L; 063 } 064 065 public static class CheckedException2 extends Exception { 066 private static final long serialVersionUID = 1L; 067 } 068 069 @Privileged 070 static int throwingCheckedException(int which, String propertyToGet) throws CheckedException1, CheckedException2 { 071 System.getProperty(propertyToGet); 072 switch (which) { 073 case 1: 074 throw new CheckedException1(); 075 case 2: 076 throw new CheckedException2(); 077 default: 078 return which; 079 } 080 } 081}