aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/InheritableThreadLocal.java
blob: 2dc85f7890ad812268d7039c06fad617b0f473ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* java.lang.InheritableThreadLocal
   Copyright (C) 2000, 2001 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.lang;

import java.util.Iterator;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * ThreadLocal whose value is inherited by child Threads.
 * The value of the InheritableThreadLocal associated with the (parent) Thread
 * on the moment that it creates a new (child) Thread is set as the value that
 * is associated with the new (child) Thread.
 * <p>
 * It is possible to make the value associated with the child Thread a function
 * of the value that is associated with the parent Thread by overriding the
 * <code>childValue()</code> method.
 *
 * @since 1.2
 * @author Mark Wielaard (mark@klomp.org)
 */
public class InheritableThreadLocal extends ThreadLocal {
	
	/**
	 * Maps Threads to a Set of InheritableThreadLocals
	 * (the heritage of that Thread).
	 * Uses a WeakHashMap so if the Thread is garbage collected the reference
	 * to that Set disappears.
	 * Both <code>AddToHeritage</code> access and modify it so they have to
	 * synchronize on the threadMap when they do.
	 */
	private static Map threadMap = new WeakHashMap();
	
	/**
	 * Creates a new InheritableThreadLocal that has no values associated
	 * with it yet.
	 */
	public InheritableThreadLocal() {
		super();
	}
	
	/**
	 * Determines the value associated with a newly created child Thread
	 * as a function of the value associated with the currently executing
	 * (parent) Thread.
	 * <p>
	 * The default implementation just returns the parentValue.
	 */
	protected Object childValue(Object parentValue) {
		return parentValue;
	}
	
	/**
	 * Adds this <code>InheritableThreadLocal</code> to the heritage of the
	 * current Thread and returns the value of the <code>ThreadLocal</code>
	 * for the Thread. The value will be either the last value that the
	 * current Thread has set, or the childValue of the last value that the
	 * parent Thread set before the current Thread was created, or the
	 * initialValue of the <code>ThreadLocal</code>.
	 *
	 * @see ThreadLocal#get()
	 */
	public Object get() {
		addToHeritage(); 
		return super.get();
	}
	
	/**
	 * Adds this <code>InheritableThreadLocal</code> to the heritage of the
	 * current Thread and sets the value of the <code>ThreadLocal</code>
	 * for the Thread.
	 *
	 * @see ThreadLocal#set(Object)
	 */
	public void set(Object value) {
		addToHeritage();
		super.set(value);
	}
	
	/**
	 * Adds this <code>InheritableThreadLocal</code> to the heritage
	 * of the current Thread.
	 */
	private void addToHeritage() {
		Thread currentThread = Thread.currentThread();
		Set heritage;
		synchronized(threadMap) {
			heritage = (Set)threadMap.get(currentThread);
		}
		// Note that we don't have to synchronize on the heritage Set
		// since only this Thread (or the parent Thread when creating
		// the heritage) ever modifies it.
		if (heritage == null) {
			heritage = new HashSet();
			synchronized(threadMap) {
				threadMap.put(currentThread, heritage);
			}
		}
		if (!heritage.contains(this)) {
			heritage.add(this);
		}
	}
	
	/**
	 * Generates the childValues of all <code>InheritableThreadLocal</code>s
	 * that are in the heritage of the current Thread for the newly created
	 * childThread.
	 * Should be called from the contructor of java.lang.Thread.
	 */
	static void newChildThread(Thread childThread) {
		// The currentThread is the parent of the new thread
		Thread parentThread = Thread.currentThread();
		
		// Inherit all the InheritableThreadLocals of the parent thread
		Set heritage;
		synchronized(threadMap) {
			heritage = (Set)threadMap.get(parentThread);
		}
		// Note that we don't have to synchronize on the heritage Set
		// since only this Thread (or the parent Thread when creating
		// the heritage) ever modifies it.
		if (heritage != null) {
			synchronized(threadMap) {
				threadMap.put(childThread, new HashSet(heritage));
			}
			// And constructs all the new child values
			// (has to be done now that we are executing in the parentThread)
			Iterator it = heritage.iterator();
			while (it.hasNext()) {
				InheritableThreadLocal local =
					(InheritableThreadLocal) it.next();
				// Note that the parentValue cannot be null
				// If it was it would not be in the heritage
				Object parentValue = local.get(parentThread).getValue();
				Object childValue = local.childValue(parentValue);
				ThreadLocal.Value v = new ThreadLocal.Value(childValue);
				local.set(childThread, v);
			}
		}
	}
}
N IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ #ifndef OPENSSL_NO_ECDSA #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "apps.h" #include <openssl/bio.h> #include <openssl/err.h> #include <openssl/ecdsa.h> #include <openssl/evp.h> #include <openssl/x509.h> #include <openssl/pem.h> #undef PROG #define PROG ecdsa_main /* -inform arg - input format - default PEM (one of DER, NET or PEM) * -outform arg - output format - default PEM * -in arg - input file - default stdin * -out arg - output file - default stdout * -des - encrypt output if PEM format with DES in cbc mode * -des3 - encrypt output if PEM format * -idea - encrypt output if PEM format * -aes128 - encrypt output if PEM format * -aes192 - encrypt output if PEM format * -aes256 - encrypt output if PEM format * -text - print a text version * -pub - print the ECDSA public key * -compressed - print the public key in compressed form ( default ) * -hybrid - print the public key in hybrid form * -uncompressed - print the public key in uncompressed form * the last three options ( compressed, hybrid and uncompressed ) * are only used if the "-pub" option is also selected. * For a precise description of the the meaning of compressed, * hybrid and uncompressed please refer to the X9.62 standart. * All three forms represents ways to express the ecdsa public * key ( a point on a elliptic curve ) as octet string. Let len be * the length ( in bytes ) of an element of the field over which * the curve is defined, then a compressed octet string has the form * 0x02 + result of BN_bn2bin() of the x coordinate of the public key */ int MAIN(int, char **); int MAIN(int argc, char **argv) { ENGINE *e = NULL; int ret = 1; ECDSA *ecdsa = NULL; int i, badops = 0; const EVP_CIPHER *enc = NULL; BIO *in = NULL, *out = NULL; int informat, outformat, text=0, noout=0; int pubin = 0, pubout = 0; char *infile, *outfile, *prog, *engine; char *passargin = NULL, *passargout = NULL; char *passin = NULL, *passout = NULL; int pub = 0, point_form = 0; unsigned char *buffer = NULL; unsigned int buf_len = 0; BIGNUM *tmp_bn = NULL; apps_startup(); if (bio_err == NULL) if ((bio_err=BIO_new(BIO_s_file())) != NULL) BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); if (!load_config(bio_err, NULL)) goto end; engine = NULL; infile = NULL; outfile = NULL; informat = FORMAT_PEM; outformat = FORMAT_PEM; prog = argv[0]; argc--; argv++; while (argc >= 1) { if (strcmp(*argv,"-inform") == 0) { if (--argc < 1) goto bad; informat=str2fmt(*(++argv)); } else if (strcmp(*argv,"-outform") == 0) { if (--argc < 1) goto bad; outformat=str2fmt(*(++argv)); } else if (strcmp(*argv,"-in") == 0) { if (--argc < 1) goto bad; infile= *(++argv); } else if (strcmp(*argv,"-out") == 0) { if (--argc < 1) goto bad; outfile= *(++argv); } else if (strcmp(*argv,"-passin") == 0) { if (--argc < 1) goto bad; passargin= *(++argv); } else if (strcmp(*argv,"-passout") == 0) { if (--argc < 1) goto bad; passargout= *(++argv); } else if (strcmp(*argv, "-engine") == 0) { if (--argc < 1) goto bad; engine= *(++argv); } else if (strcmp(*argv, "-noout") == 0) noout = 1; else if (strcmp(*argv, "-text") == 0) text = 1; else if (strcmp(*argv, "-pub") == 0) { pub = 1; buffer = (unsigned char *)(*(argv+1)); if (strcmp((char *)buffer, "compressed") == 0) point_form = POINT_CONVERSION_COMPRESSED; else if (strcmp((char *)buffer, "hybrid") == 0) point_form = POINT_CONVERSION_HYBRID; else if (strcmp((char *)buffer, "uncompressed") == 0) point_form = POINT_CONVERSION_UNCOMPRESSED; if (point_form) { argc--; argv++; } } else if (strcmp(*argv, "-pubin") == 0) pubin=1; else if (strcmp(*argv, "-pubout") == 0) pubout=1; else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL) { BIO_printf(bio_err,"unknown option %s\n",*argv); badops=1; break; } argc--; argv++; } if (badops) { bad: BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog); BIO_printf(bio_err, "where options are\n"); BIO_printf(bio_err, " -inform arg input format - DER or PEM\n"); BIO_printf(bio_err, " -outform arg output format - DER or PEM\n"); BIO_printf(bio_err, " -in arg input file\n"); BIO_printf(bio_err, " -passin arg input file pass phrase source\n"); BIO_printf(bio_err, " -out arg output file\n"); BIO_printf(bio_err, " -passout arg output file pass phrase source\n"); BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n"); BIO_printf(bio_err, " -des encrypt PEM output with cbc des\n"); BIO_printf(bio_err, " -des3 encrypt PEM output with ede cbc des using 168 bit key\n"); #ifndef OPENSSL_NO_IDEA BIO_printf(bio_err, " -idea encrypt PEM output with cbc idea\n"); #endif #ifndef OPENSSL_NO_AES BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); BIO_printf(bio_err, " encrypt PEM output with cbc aes\n"); #endif BIO_printf(bio_err, " -text print the key in text\n"); BIO_printf(bio_err, " -noout don't print key out\n"); BIO_printf(bio_err, " -pub [compressed | hybrid | uncompressed] \n"); BIO_printf(bio_err, " compressed print the public key in compressed form ( default )\n"); BIO_printf(bio_err, " hybrid print the public key in hybrid form\n"); BIO_printf(bio_err, " uncompressed print the public key in uncompressed form\n"); goto end; } ERR_load_crypto_strings(); e = setup_engine(bio_err, engine, 0); if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); goto end; } in = BIO_new(BIO_s_file()); out = BIO_new(BIO_s_file()); if ((in == NULL) || (out == NULL)) { ERR_print_errors(bio_err); goto end; } if (infile == NULL) BIO_set_fp(in,stdin,BIO_NOCLOSE); else { if (BIO_read_filename(in,infile) <= 0) { perror(infile); goto end; } } BIO_printf(bio_err,"read ECDSA key\n"); if (informat == FORMAT_ASN1) { if (pubin) ecdsa = d2i_ECDSA_PUBKEY_bio(in, NULL); else ecdsa = d2i_ECDSAPrivateKey_bio(in, NULL); } else if (informat == FORMAT_PEM) { if (pubin) ecdsa = PEM_read_bio_ECDSA_PUBKEY(in, NULL, NULL, NULL); else ecdsa = PEM_read_bio_ECDSAPrivateKey(in, NULL, NULL, passin); } else { BIO_printf(bio_err, "bad input format specified for key\n"); goto end; } if (ecdsa == NULL) { BIO_printf(bio_err,"unable to load Key\n"); ERR_print_errors(bio_err); goto end; } if (outfile == NULL) { BIO_set_fp(out, stdout, BIO_NOCLOSE); #ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } #endif } else { if (BIO_write_filename(out, outfile) <= 0) { perror(outfile); goto end; } } if (text) if (!ECDSA_print(out, ecdsa, 0)) { perror(outfile); ERR_print_errors(bio_err); goto end; } if (pub) { fprintf(stdout, "Public Key ("); if (point_form == POINT_CONVERSION_COMPRESSED) fprintf(stdout, "COMPRESSED"); else if (point_form == POINT_CONVERSION_UNCOMPRESSED) fprintf(stdout, "UNCOMPRESSED"); else if (point_form == POINT_CONVERSION_HYBRID) fprintf(stdout, "HYBRID"); fprintf(stdout, ")="); buf_len = EC_POINT_point2oct(ecdsa->group, EC_GROUP_get0_generator(ecdsa->group), point_form, NULL, 0, NULL); if (!buf_len) { BIO_printf(bio_err,"invalid public key length\n"); ERR_print_errors(bio_err); goto end; } if ((tmp_bn = BN_new()) == NULL || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto end; if (!EC_POINT_point2oct(ecdsa->group, EC_GROUP_get0_generator(ecdsa->group), point_form, buffer, buf_len, NULL) || !BN_bin2bn(buffer, buf_len, tmp_bn)) { BIO_printf(bio_err,"can not encode public key\n"); ERR_print_errors(bio_err); OPENSSL_free(buffer); goto end; } BN_print(out, tmp_bn); fprintf(stdout,"\n"); } if (noout) goto end; BIO_printf(bio_err, "writing ECDSA key\n"); if (outformat == FORMAT_ASN1) { if(pubin || pubout) i = i2d_ECDSA_PUBKEY_bio(out, ecdsa); else i = i2d_ECDSAPrivateKey_bio(out, ecdsa); } else if (outformat == FORMAT_PEM) { if(pubin || pubout) i = PEM_write_bio_ECDSA_PUBKEY(out, ecdsa); else i = PEM_write_bio_ECDSAPrivateKey(out, ecdsa, enc, NULL, 0, NULL, passout); } else { BIO_printf(bio_err, "bad output format specified for outfile\n"); goto end; } if (!i) { BIO_printf(bio_err, "unable to write private key\n"); ERR_print_errors(bio_err); } else ret=0; end: if (in) BIO_free(in); if (out) BIO_free_all(out); if (ecdsa) ECDSA_free(ecdsa); if (tmp_bn) BN_free(tmp_bn); if (passin) OPENSSL_free(passin); if (passout) OPENSSL_free(passout); apps_shutdown(); EXIT(ret); } #endif